- 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 - sortedIndexOf 方法
語法
_.sortedIndex(array, value, [iteratee], [context])
sortedIndex 方法使用二分查詢演算法向陣列中插入一個值以保持排序順序,並返回索引。如果存在遍歷器函式,它將被用來計算索引。
示例
var _ = require('underscore');
var list = [1, 2, 4, 5, 6]
//Example: get sorted index of 3
result = _.sortedIndex(list, 3);
console.log(result)
list = [{name: 'Joe', age: 40}, {name: 'Rob', age: 60}];
//Example: get sorted index of new object
result = _.sortedIndex(list, {name: 'Julie', age: 50}, 'age' );
console.log(result)
將以上程式儲存在 tester.js 中。執行以下命令執行此程式。
命令
\>node tester.js
輸出
2 1
underscorejs_iterating_array.htm
廣告