jquery - Removing Multiple Objects from Javascript Array Breaks Half Way Through -
i have array of few hundred json objects...
var self.collection = [object, object, object, object, object, object…]
each 1 looks this...
0: object id: "25093712" name: "john haberstich"
i'm iterating through array searching each array.id see if matches ids in second array...
var fbcontactids = ["1072980313", "2502342", "2509374", "2524864", "2531941"] $.each(self.collection, function(index, k) { if (fbcontactids.indexof(k.id) > -1) { self.collection.splice(index, 1); }; });
however code works splice 3 of objects self.collection array , breaks , gives following error:
uncaught typeerror: cannot read property 'id' of undefined
the line causing error one...
if (fbcontactids.indexof(k.id) > -1) {
could tell me i'm dong wrong here?
because length of collection change, trick loop rear front
for (var index = self.collection.length - 1; index >= 0; index--) { k = self.collection[index]; if (fbcontactids.indexof(k.id) > -1) { self.collection.splice(index, 1); }; }
Comments
Post a Comment