1

What is the easiest way to remove all objects from an array with a particular property = x?

3
  • 2
    What kind of array? Sample please Commented Dec 23, 2013 at 7:03
  • _.without(array, [*values]) _.without([1, 2, 1, 0, 3, 1, 4], 0, 1); Commented Dec 23, 2013 at 7:06
  • possible duplicate of Remove array element based on object property Commented Dec 23, 2013 at 7:10

2 Answers 2

2

Use _.filter

var result = _.filter(arr, function(item) {
  return !("prop" in item);
});

If you want to limit it to the immediate properties of each item, use

var result = _.filter(arr, function(item) {
  return !item.hasOwnProperty("prop");
});
Sign up to request clarification or add additional context in comments.

Comments

0

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'; });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.