mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-03 16:47:49 +00:00
Reimplement find.
This commit is contained in:
13
find.js
13
find.js
@@ -1,5 +1,6 @@
|
||||
import createFind from './.internal/createFind.js'
|
||||
import findIndex from './findIndex.js'
|
||||
import isArrayLike from './isArrayLike'
|
||||
import keys from './keys'
|
||||
|
||||
/**
|
||||
* Iterates over elements of `collection`, returning the first element
|
||||
@@ -24,6 +25,14 @@ import findIndex from './findIndex.js'
|
||||
* find(users, ({ age }) => age < 40)
|
||||
* // => object for 'barney'
|
||||
*/
|
||||
const find = createFind(findIndex)
|
||||
function find(collection, predicate, fromIndex) {
|
||||
var iterable = Object(collection);
|
||||
if (!isArrayLike(collection)) {
|
||||
collection = keys(collection);
|
||||
predicate = function(key) { return iteratee(iterable[key], key, iterable); };
|
||||
}
|
||||
var index = findIndex(collection, predicate, fromIndex);
|
||||
return index > -1 ? iterable[iteratee ? collection[index] : index] : undefined;
|
||||
}
|
||||
|
||||
export default find
|
||||
|
||||
Reference in New Issue
Block a user