findIndex source npm
_.findIndex(array, [predicate=_.identity])
This method is like _.find
except that it returns the index of the first
element predicate
returns truthy for instead of the element itself.
Arguments
- array (Array)
The array to search.
- [predicate=_.identity] (Function|Object|string)
The function invoked per iteration.
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(users, function(o) { return o.user == 'barney'; });
// => 0
// using the `_.matches` callback shorthand
_.findIndex(users, { 'user': 'fred', 'active': false });
// => 1
// using the `_.matchesProperty` callback shorthand
_.findIndex(users, ['active', false]);
// => 0
// using the `_.property` callback shorthand
_.findIndex(users, 'active');
// => 2