- Underscore.JS 教程
- Underscore.JS - 主页
- Underscore.JS - 概述
- Underscore.JS - 环境设置
- Underscore.JS - 迭代集合
- Underscore.JS - 处理集合
- Underscore.JS - 迭代数组
- Underscore.JS - 处理数组
- Underscore.JS - 函数
- Underscore.JS - 映射对象
- Underscore.JS - 更新对象
- Underscore.JS - 比较对象
- Underscore.JS - 实用工具
- Underscore.JS - 链接
- Underscore.JS 有用资源
- Underscore.JS - 快速指南
- Underscore.JS - 实用工具
- Underscore.JS - 讨论
Underscore.JS - 方法函数
语法
_.findKey(object, predicate, [context])
findKey 方法返回这是谓词在 key 上为 true 的对象的 key。参阅以下示例 -
示例
var _ = require('underscore');
var users = [{'id': 1, 'name': 'Sam', 'last': 'Brown'},
{'id': 2, 'name': 'Joe', 'last': 'Black'},
{'id': 3, 'name': 'Julie', 'last': 'Jordon'},
{'id': 4, 'name': 'Tim', 'last': 'Jane'}];
// Get index of first user whose id is even
var result = _.findKey(users, function(user){ return user.id % 2 == 0});
console.log(result);
将以上程序另存为 tester.js。运行以下命令来执行此程序。
命令
\>node tester.js
输出
1
underscorejs_mapping_objects.htm
广告