find.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. var createFind = require('./_createFind'),
  2. findIndex = require('./findIndex');
  3. /**
  4. * Iterates over elements of `collection`, returning the first element
  5. * `predicate` returns truthy for. The predicate is invoked with three
  6. * arguments: (value, index|key, collection).
  7. *
  8. * @static
  9. * @memberOf _
  10. * @since 0.1.0
  11. * @category Collection
  12. * @param {Array|Object} collection The collection to inspect.
  13. * @param {Function} [predicate=_.identity] The function invoked per iteration.
  14. * @param {number} [fromIndex=0] The index to search from.
  15. * @returns {*} Returns the matched element, else `undefined`.
  16. * @example
  17. *
  18. * var users = [
  19. * { 'user': 'barney', 'age': 36, 'active': true },
  20. * { 'user': 'fred', 'age': 40, 'active': false },
  21. * { 'user': 'pebbles', 'age': 1, 'active': true }
  22. * ];
  23. *
  24. * _.find(users, function(o) { return o.age < 40; });
  25. * // => object for 'barney'
  26. *
  27. * // The `_.matches` iteratee shorthand.
  28. * _.find(users, { 'age': 1, 'active': true });
  29. * // => object for 'pebbles'
  30. *
  31. * // The `_.matchesProperty` iteratee shorthand.
  32. * _.find(users, ['active', false]);
  33. * // => object for 'fred'
  34. *
  35. * // The `_.property` iteratee shorthand.
  36. * _.find(users, 'active');
  37. * // => object for 'barney'
  38. */
  39. var find = createFind(findIndex);
  40. module.exports = find;