Get first element of a collection that matches iterator function

You can use find:

Looks through each value in the list, returning the first one that
passes a truth test (iterator), or undefined if no value passes the
test. The function returns as soon as it finds an acceptable element,
and doesn’t traverse the entire list.

Using your example:

var g = _.find(arr, function (x) { return x.a > 10 })

See the main page:

Another thing to note (which might be your question) is the chain function to join calls together:

var g = _.chain(arr).filter(function (x) { return x.a > 10 }).first().value()

Notice the calls to filter and `first’ which can follow each other without any nesting.

Leave a Comment