codecamp

Lodash _.findKey

_.findKey(object, [predicate=_.identity])

这个方法类似_.find 。 除了它返回最先被 predicate 判断为真值的元素 key,而不是元素本身。

添加版本

1.1.0

参数

  1. object (Object): 需要检索的对象。
  2. [predicate=_.identity] (Function): 每次迭代时调用的函数。

返回

(*): 返回匹配的 key,否则返回 undefined。

例子

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'


Lodash _.extendWith->assignInWith
Lodash _.findLastKey
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

Lodash 简介

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }