UNPKG

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