I would like to achieve something like _.first
with _.filter
, that is, having a collection of elements, I'd like to get the first one (if exists) that matches a truth test (iterator).
For example, given an array like the following:
var arr = [{a: 1}, {a: 5}, {a: 9}, {a: 11}, {a: 15}]
I would like to getthe first (and only first) element that matches my custom function:
_.filterFirst(arr, function(el) { return el.a > 10; }); // make it
So far:
_.first(arr) == {a:1}
_.filter(arr, function(...)) == [{a:11}, {a:15}]
Is there a clean solution to do this which is better than _.first(_.filter(arr, iterator))
?