Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lodash _.find all matches

I have simple function to return me object which meets my criteria.

Code looks like:

    var res = _.find($state.get(), function(i) {         var match = i.name.match(re);         return match &&             (!i.restrict || i.restrict($rootScope.user));     }); 

How can I find all results (not just first) which meets this criteria but all results.

Thanks for any advise.

like image 723
Andurit Avatar asked Feb 19 '16 10:02

Andurit


People also ask

What is _ get?

Overview. The _. get() method in Lodash retrieves the object's value at a specific path. If the value is not present at the object's specific path, it will be resolved as undefined . This method will return the default value if specified in such a case.

What is _ find in JavaScript?

The _. find() function looks at each element of the list and returns the first occurrence of the element that satisfy the condition. If any element of list is not satisfy the condition then it returns the undefined value. Syntax: _.find(list, predicate, [context])

How do I compare two arrays in Lodash?

isEqual() Method. The Lodash _. isEqual() Method performs a deep comparison between two values to determine if they are equivalent. This method supports comparing arrays, array buffers, boolean, date objects, maps, numbers, objects, regex, sets, strings, symbols, and typed arrays.


1 Answers

Just use _.filter - it returns all matched items.

_.filter

Iterates over elements of collection, returning an array of all elements predicate returns truthy for. The predicate is invoked with three arguments: (value, index|key, collection).

like image 185
stasovlas Avatar answered Sep 20 '22 03:09

stasovlas