- 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
廣告