Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lodash.without function that remove object with specific field

I'm familiar with _.without function

This will remove specific values from an array:

_.without([1, 2, 1, 3], 1, 2);
// → [3]

Is there a built-in / lodash function (or - how can I implement an efficient one) that remove not a specific value but a var with a specified field value/

_.without([ { number: 1}, {number: 2} ], 1)
// -> [ {number: 2} ]
like image 335
Shikloshi Avatar asked Oct 26 '15 22:10

Shikloshi


People also ask

How do I remove a property from object Lodash?

unset() method is used to remove the property at the path of the object. If the property is removed then it returns True value otherwise, it returns False.

How do I remove an object from an array using Lodash?

remove() Method. The _. remove() method is used to remove all elements from the array that predicate returns True and returns the removed elements.

What is _ get?

The _. get() function is an inbuilt function in the Underscore. js library of JavaScript which is used to get the value at the path of object. If the resolved value is undefined, the defaultValue is returned in its place. Syntax: _.get(object, path, [defaultValue])

What is Noop Lodash?

The Lodash _. noop() method is used to return “undefined” irrespective of the arguments passed to it. Syntax: _.noop() Parameters: This method can take optional parameters of any type. Returns: This method returns undefined.


1 Answers

You can use _.filter:

_.filter([ { number: 1}, {number: 2} ], (o) => o.number != 1)

or, without the new arrow notation:

_.filter([ { number: 1}, {number: 2} ], function (o) { return o.number != 1 })
like image 120
royhowie Avatar answered Sep 24 '22 21:09

royhowie