mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 07:47:49 +00:00
Ensure _.find and _.findLast pass the correct key param to predicate. [closes #2367]
This commit is contained in:
37
lodash.js
37
lodash.js
@@ -4549,6 +4549,31 @@
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a `_.find` or `_.findLast` function.
|
||||
*
|
||||
* @private
|
||||
* @param {Function} findIndexFunc The function to find the collection index.
|
||||
* @returns {Function} Returns the new find function.
|
||||
*/
|
||||
function createFind(findIndexFunc) {
|
||||
return function(collection, predicate, fromIndex) {
|
||||
var iterable = Object(collection);
|
||||
predicate = getIteratee(predicate, 3);
|
||||
if (!isArrayLike(collection)) {
|
||||
var props = keys(collection);
|
||||
}
|
||||
var index = findIndexFunc(props || collection, function(value, key) {
|
||||
if (props) {
|
||||
key = value;
|
||||
value = iterable[key];
|
||||
}
|
||||
return predicate(value, key, iterable);
|
||||
}, fromIndex);
|
||||
return index > -1 ? collection[props ? props[index] : index] : undefined;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a `_.flow` or `_.flowRight` function.
|
||||
*
|
||||
@@ -8472,11 +8497,7 @@
|
||||
* _.find(users, 'active');
|
||||
* // => object for 'barney'
|
||||
*/
|
||||
function find(collection, predicate, fromIndex) {
|
||||
collection = isArrayLike(collection) ? collection : values(collection);
|
||||
var index = findIndex(collection, predicate, fromIndex);
|
||||
return index > -1 ? collection[index] : undefined;
|
||||
}
|
||||
var find = createFind(findIndex);
|
||||
|
||||
/**
|
||||
* This method is like `_.find` except that it iterates over elements of
|
||||
@@ -8498,11 +8519,7 @@
|
||||
* });
|
||||
* // => 3
|
||||
*/
|
||||
function findLast(collection, predicate, fromIndex) {
|
||||
collection = isArrayLike(collection) ? collection : values(collection);
|
||||
var index = findLastIndex(collection, predicate, fromIndex);
|
||||
return index > -1 ? collection[index] : undefined;
|
||||
}
|
||||
var findLast = createFind(findLastIndex);
|
||||
|
||||
/**
|
||||
* Creates a flattened array of values by running each element in `collection`
|
||||
|
||||
Reference in New Issue
Block a user