- 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 - sortBy 方法
語法
_.sortBy(list, iteratee, [context])
sortBy 方法透過執行提供的 iteratee 方法獲取排序的列表。
示例
var _ = require('underscore');
var list = [{name: 'Sam', age: 10}, {name: 'Joe', age: 12}, {name: 'Rob', age: 15}]
//Example 1. invoke sortBy method to get sorted list of students
var result = _.sortBy(list, 'name');
console.log(result);
list = [1, 4, 6, 3, 7, 9]
//Example 2. invoke sortBy method to get sorted list of number
result = _.sortBy(list)
console.log(result)
將以上程式儲存在 tester.js 中。執行以下命令以執行該程式。
命令
\>node tester.js
輸出
[
{ name: 'Joe', age: 12 },
{ name: 'Rob', age: 15 },
{ name: 'Sam', age: 10 }
]
[ 1, 3, 4, 6, 7, 9 ]
underscorejs_processing_collection
廣告