Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var baseForOwn = require('../internal/baseForOwn'),
2
createFindKey = require('../internal/createFindKey');
3
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
* If a property name is provided for `predicate` the created `_.property`
9
* style callback returns the property value of the given element.
10
*
11
* If a value is also provided for `thisArg` the created `_.matchesProperty`
12
* style callback returns `true` for elements that have a matching property
13
* value, else `false`.
14
*
15
* If an object is provided for `predicate` the created `_.matches` style
16
* callback returns `true` for elements that have the properties of the given
17
* object, else `false`.
18
*
19
* @static
20
* @memberOf _
21
* @category Object
22
* @param {Object} object The object to search.
23
* @param {Function|Object|string} [predicate=_.identity] The function invoked
24
* per iteration.
25
* @param {*} [thisArg] The `this` binding of `predicate`.
26
* @returns {string|undefined} Returns the key of the matched element, else `undefined`.
27
* @example
28
*
29
* var users = {
30
* 'barney': { 'age': 36, 'active': true },
31
* 'fred': { 'age': 40, 'active': false },
32
* 'pebbles': { 'age': 1, 'active': true }
33
* };
34
*
35
* _.findKey(users, function(chr) {
36
* return chr.age < 40;
37
* });
38
* // => 'barney' (iteration order is not guaranteed)
39
*
40
* // using the `_.matches` callback shorthand
41
* _.findKey(users, { 'age': 1, 'active': true });
42
* // => 'pebbles'
43
*
44
* // using the `_.matchesProperty` callback shorthand
45
* _.findKey(users, 'active', false);
46
* // => 'fred'
47
*
48
* // using the `_.property` callback shorthand
49
* _.findKey(users, 'active');
50
* // => 'barney'
51
*/
52
var findKey = createFindKey(baseForOwn);
53
54
module.exports = findKey;
55
56