mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 02:17:50 +00:00
Simplify _.find by having it use _.findIndex.
This commit is contained in:
19
lodash.js
19
lodash.js
@@ -1392,7 +1392,8 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* The base implementation of `find`, 'findLast`, `findKey`, and `findLastKey`
|
* The base implementation of `find`, 'findLast`, `findKey`, and `findLastKey`
|
||||||
* without support for callback shorthands or `this` binding.
|
* without support for callback shorthands or `this` binding which iterates
|
||||||
|
* over `collection` using the provided `eachFunc`.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {Array|Object|string} collection The collection to search.
|
* @param {Array|Object|string} collection The collection to search.
|
||||||
@@ -3821,20 +3822,12 @@
|
|||||||
* // => { 'name': 'fred', 'age': 40, 'blocked': true }
|
* // => { 'name': 'fred', 'age': 40, 'blocked': true }
|
||||||
*/
|
*/
|
||||||
function find(collection, predicate, thisArg) {
|
function find(collection, predicate, thisArg) {
|
||||||
predicate = lodash.createCallback(predicate, thisArg, 3);
|
|
||||||
if (isArray(collection)) {
|
if (isArray(collection)) {
|
||||||
var index = -1,
|
var index = findIndex(collection, predicate, thisArg);
|
||||||
length = collection.length;
|
return index > -1 ? collection[index] : undefined;
|
||||||
|
|
||||||
while (++index < length) {
|
|
||||||
var value = collection[index];
|
|
||||||
if (predicate(value, index, collection)) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return baseFind(collection, predicate, baseEach);
|
|
||||||
}
|
}
|
||||||
|
predicate = lodash.createCallback(predicate, thisArg, 3);
|
||||||
|
return baseFind(collection, predicate, baseEach);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user