find source npm

_.find(collection, [predicate=_.identity])

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

Arguments

  1. collection (Array|Object)

    The collection to search.

  2. [predicate=_.identity] (Function|Object|string)

    The function invoked per iteration.

Returns (*)

Returns the matched element, else undefined.

Example

var resolve = _.partial(_.result, _, 'user');

var users = [
  { 'user': 'barney',  'age': 36, 'active': true },
  { 'user': 'fred',    'age': 40, 'active': false },
  { 'user': 'pebbles', 'age': 1,  'active': true }
];

resolve( _.find(users, function(o) { return o.age < 40; }) );
// => 'barney'

// using the `_.matches` callback shorthand
resolve( _.find(users, { 'age': 1, 'active': true }) );
// => 'pebbles'

// using the `_.matchesProperty` callback shorthand
resolve( _.find(users, ['active', false]) );
// => 'fred'

// using the `_.property` callback shorthand
resolve( _.find(users, 'active') );
// => 'barney'