Search code examples
javascriptlodash

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.


Solution

  • 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).