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:
50
find.js
Normal file
50
find.js
Normal file
@@ -0,0 +1,50 @@
|
||||
import baseEach from './internal/baseEach';
|
||||
import baseFind from './internal/baseFind';
|
||||
import baseFindIndex from './internal/baseFindIndex';
|
||||
import baseIteratee from './internal/baseIteratee';
|
||||
import isArray from './isArray';
|
||||
|
||||
/**
|
||||
* Iterates over elements of `collection`, returning the first element
|
||||
* `predicate` returns truthy for. The predicate is invoked with three arguments:
|
||||
* (value, index|key, collection).
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collection
|
||||
* @param {Array|Object} collection The collection to search.
|
||||
* @param {Function|Object|string} [predicate=_.identity] The function invoked per iteration.
|
||||
* @returns {*} Returns the matched element, else `undefined`.
|
||||
* @example
|
||||
*
|
||||
* var users = [
|
||||
* { 'user': 'barney', 'age': 36, 'active': true },
|
||||
* { 'user': 'fred', 'age': 40, 'active': false },
|
||||
* { 'user': 'pebbles', 'age': 1, 'active': true }
|
||||
* ];
|
||||
*
|
||||
* _.find(users, function(o) { return o.age < 40; });
|
||||
* // => object for 'barney'
|
||||
*
|
||||
* // using the `_.matches` iteratee shorthand
|
||||
* _.find(users, { 'age': 1, 'active': true });
|
||||
* // => object for 'pebbles'
|
||||
*
|
||||
* // using the `_.matchesProperty` iteratee shorthand
|
||||
* _.find(users, ['active', false]);
|
||||
* // => object for 'fred'
|
||||
*
|
||||
* // using the `_.property` iteratee shorthand
|
||||
* _.find(users, 'active');
|
||||
* // => object for 'barney'
|
||||
*/
|
||||
function find(collection, predicate) {
|
||||
predicate = baseIteratee(predicate, 3);
|
||||
if (isArray(collection)) {
|
||||
var index = baseFindIndex(collection, predicate);
|
||||
return index > -1 ? collection[index] : undefined;
|
||||
}
|
||||
return baseFind(collection, predicate, baseEach);
|
||||
}
|
||||
|
||||
export default find;
|
||||
Reference in New Issue
Block a user