mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-05 17:37:50 +00:00
Bump to v4.0.0.
This commit is contained in:
43
findIndex.js
Normal file
43
findIndex.js
Normal file
@@ -0,0 +1,43 @@
|
||||
import baseFindIndex from './internal/baseFindIndex';
|
||||
import baseIteratee from './internal/baseIteratee';
|
||||
|
||||
/**
|
||||
* This method is like `_.find` except that it returns the index of the first
|
||||
* element `predicate` returns truthy for instead of the element itself.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Array
|
||||
* @param {Array} array The array to search.
|
||||
* @param {Function|Object|string} [predicate=_.identity] 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` iteratee shorthand
|
||||
* _.findIndex(users, { 'user': 'fred', 'active': false });
|
||||
* // => 1
|
||||
*
|
||||
* // using the `_.matchesProperty` iteratee shorthand
|
||||
* _.findIndex(users, ['active', false]);
|
||||
* // => 0
|
||||
*
|
||||
* // using the `_.property` iteratee shorthand
|
||||
* _.findIndex(users, 'active');
|
||||
* // => 2
|
||||
*/
|
||||
function findIndex(array, predicate) {
|
||||
return (array && array.length)
|
||||
? baseFindIndex(array, baseIteratee(predicate, 3))
|
||||
: -1;
|
||||
}
|
||||
|
||||
export default findIndex;
|
||||
Reference in New Issue
Block a user