- 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 - 串接方法
語法
_.chain(object)
串接方法返回一個包裹的物件,並且當呼叫此物件上的方法時,每個方法都會返回包裹的物件,直到呼叫 value 方法。請參見以下示例
示例
var _ = require('underscore');
var students = [{name: 'Sam', age: 10},{name: 'Joe', age: 8},{name: 'Rob', age: 12}]
//Get the highest aged student using chain method
var eldest = _.chain(students)
.sortBy(function(student){return student.age;})
.map(function(student){return "Name: " + student.name + ", age: " + student.age;})
.last()
.value();
console.log(eldest);
將以上程式儲存在 tester.js 中。執行以下命令以執行此程式。
命令
\>node tester.js
輸出
Name: Rob, age: 12
underscorejs_chaining.htm
廣告