What is the easiest way to remove all objects from an array with a particular property = x?
-
2What kind of array? Sample pleaseKhamidulla– Khamidulla2013-12-23 07:03:35 +00:00Commented Dec 23, 2013 at 7:03
-
_.without(array, [*values]) _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);F.C.Hsiao– F.C.Hsiao2013-12-23 07:06:07 +00:00Commented Dec 23, 2013 at 7:06
-
possible duplicate of Remove array element based on object propertyKhamidulla– Khamidulla2013-12-23 07:10:26 +00:00Commented Dec 23, 2013 at 7:10
Add a comment
|
2 Answers
It seems like the easiest way would be to use the filter method:
var newArray = _.filter(oldArray, function(x) { return !('prop' in x); });
// or
var newArray = _.filter(oldArray, function(x) { return !_.has(x, 'prop'); });
Or alternatively, the reject method should work just as well:
var newArray = _.reject(oldArray, function(x) { return 'prop' in x; });
// or
var newArray = _.reject(oldArray, function(x) { return _.has(x, 'prop'); });
Update Given your updated question, the code should look like this:
var newArray = _.filter(oldArray, function(x) { return x.property !== 'value'; });
Or like this
var newArray = _.reject(oldArray, function(x) { return x.property === 'value'; });