Skip to main content

findIndex

_.findIndex(predicate, array) # Ⓢ [Ⓣ][1] Ⓝ

This method is like _.find except that it returns the index of the first



element predicate returns truthy for instead of the element itself.

Since​

1.1.0

Arguments​

  1. predicate (Function): The function invoked per iteration.
  2. array (Array): The array to inspect.

Returns​

(number): Returns the index of the found element, else -1.

Example​

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

_.findIndex(function(o) { return o.user == 'barney'; }, users);
// => 0

// The `_.matches` iteratee shorthand.
_.findIndex({ 'user': 'fred', 'active': false }, users);
// => 1

// The `_.matchesProperty` iteratee shorthand.
_.findIndex(['active', false], users);
// => 0

// The `_.property` iteratee shorthand.
_.findIndex('active', users);
// => 2