主题
findKey
js
_.findKey(object, [predicate=_.identity])
此方法与 _.find
类似,不同之处在于它返回 predicate
返回真值的第一个元素的键,而不是元素本身。
¥This method is like _.find
except that it returns the key of the first element predicate
returns truthy for instead of the element itself.
新增于
¥Since
1.1.0
参数
¥Arguments
object
(对象):要反转的对象。¥
object
(Object): The object to inspect.[predicate=_.identity]
(函数):每次迭代调用的函数。¥
[predicate=_.identity]
(Function): The function invoked per iteration.
返回
¥Returns
(*):返回匹配元素的键,否则为 undefined
。
¥()*: Returns the key of the matched element, else undefined
.
示例
¥Example
js
var users = {
'barney': { 'age': 36, 'active': true },
'fred': { 'age': 40, 'active': false },
'pebbles': { 'age': 1, 'active': true }
};
_.findKey(users, function(o) { return o.age < 40; });
// => 'barney' (iteration order is not guaranteed)
// The `_.matches` iteratee shorthand.
_.findKey(users, { 'age': 1, 'active': true });
// => 'pebbles'
// The `_.matchesProperty` iteratee shorthand.
_.findKey(users, ['active', false]);
// => 'fred'
// The `_.property` iteratee shorthand.
_.findKey(users, 'active');
// => 'barney'