You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.3 KiB

4 years ago
  1. var baseFindKey = require('./_baseFindKey'),
  2. baseForOwn = require('./_baseForOwn'),
  3. baseIteratee = require('./_baseIteratee');
  4. /**
  5. * This method is like `_.find` except that it returns the key of the first
  6. * element `predicate` returns truthy for instead of the element itself.
  7. *
  8. * @static
  9. * @memberOf _
  10. * @since 1.1.0
  11. * @category Object
  12. * @param {Object} object The object to inspect.
  13. * @param {Function} [predicate=_.identity] The function invoked per iteration.
  14. * @returns {string|undefined} Returns the key of the matched element,
  15. * else `undefined`.
  16. * @example
  17. *
  18. * var users = {
  19. * 'barney': { 'age': 36, 'active': true },
  20. * 'fred': { 'age': 40, 'active': false },
  21. * 'pebbles': { 'age': 1, 'active': true }
  22. * };
  23. *
  24. * _.findKey(users, function(o) { return o.age < 40; });
  25. * // => 'barney' (iteration order is not guaranteed)
  26. *
  27. * // The `_.matches` iteratee shorthand.
  28. * _.findKey(users, { 'age': 1, 'active': true });
  29. * // => 'pebbles'
  30. *
  31. * // The `_.matchesProperty` iteratee shorthand.
  32. * _.findKey(users, ['active', false]);
  33. * // => 'fred'
  34. *
  35. * // The `_.property` iteratee shorthand.
  36. * _.findKey(users, 'active');
  37. * // => 'barney'
  38. */
  39. function findKey(object, predicate) {
  40. return baseFindKey(object, baseIteratee(predicate, 3), baseForOwn);
  41. }
  42. module.exports = findKey;