8
0

_baseFind.js 867 B

12345678910111213141516171819202122232425
  1. /**
  2. * The base implementation of methods like `_.find` and `_.findKey`, without
  3. * support for iteratee shorthands, which iterates over `collection` using
  4. * `eachFunc`.
  5. *
  6. * @private
  7. * @param {Array|Object} collection The collection to search.
  8. * @param {Function} predicate The function invoked per iteration.
  9. * @param {Function} eachFunc The function to iterate over `collection`.
  10. * @param {boolean} [retKey] Specify returning the key of the found element
  11. * instead of the element itself.
  12. * @returns {*} Returns the found element or its key, else `undefined`.
  13. */
  14. function baseFind(collection, predicate, eachFunc, retKey) {
  15. var result;
  16. eachFunc(collection, function(value, key, collection) {
  17. if (predicate(value, key, collection)) {
  18. result = retKey ? key : value;
  19. return false;
  20. }
  21. });
  22. return result;
  23. }
  24. export default baseFind;