I’m filtering an array which forms a set of markers on a Google Map (API V3) here: http://testdae.dialanexchange.com/testmap.aspx.
It all works in combination correctly for either of Timeshare Resorts and Private Properties with either or both of Disabled Access and Allow Pets. In all these cases the debug alert I’ve put in shows 1642 which is the number of points in the original array used for the markers.
However, when you select All and either or both of Disabled Access and Allow Pets, after it has correctly displayed the result, deselecting either of the checkboxes results in the alert showing that these filters have affected the original array as it indicates the number of the Total at the bottom of the filter section. It can only be reset by reloading the page.
I think the issue is in the filterProperties() function but I can’t see it.
Is there something I’m missing here because I’ve been staring at it, Googling and trying various things to get it to work for over a day now? I thought $.grep wasn’t supposed to affect the original array.
The answer turned out to be that it wasn’t $.grep that was at fault but that assigning an object to another object in Javascript causes both objects to be altered if one is, as they’re linked by reference to the same object. The solution was to clone a new object from the original, thus maintaining the integrity of the original object. References are:-
1) What is the most efficient way to deep clone an object in JavaScript?
2) http://od-eon.com/blogs/bogdan/javascript-assignment-reference-vs-object-cloning/