Reputation: 27703
So I have something like this defined
var myList = [];
//I populate it like this
myList.push({ prop1: value1,
prop2: value2,
prop3: value3
});
Is it possible to remove items from the list, based on prop1 value w/o searching through the array?
Upvotes: 0
Views: 163
Reputation: 774
Is it required that myList have index of integral type? Might you instead use key => value pairs - that is, an associative array with the value of prop1 as the key? How likely is it that prop1 will be unique? If it's unlikely, consider implementing a multimap.
Upvotes: 0
Reputation: 185883
Here you go:
myList = myList.filter( function ( obj ) {
return obj.prop1 !== value;
});
where value
is the value that you're testing against.
Live demo: http://jsfiddle.net/LBYfa/
So, if the value of the 'prop1'
property is equal to the value that you're testing against, obj.prop1 !== value
will evaluate to false
and that element will be filtered out.
Upvotes: 1
Reputation: 100175
Does this help:
Array.prototype.remove = function(s) { var i = this.indexOf(s); if(i != -1) this.splice(i, 1); }
Upvotes: 0
Reputation: 19550
No. You must loop over the array in some fashion, checking the properties of each object within, and perform removal as you hit them.
Upvotes: 1