- 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 - map 方法
語法
_.map(list, iteratee, [context])
map 方法透過對映元素列表上的每個值來生成新的值陣列,同時迭代給定的元素列表,呼叫繫結到上下文物件的迭代器函式(如果已傳遞)。迭代器使用三個引數呼叫:(element, index, list)。對於 JavaScript 物件,迭代器的物件將是 (value, key, list)。返回列表以供鏈式處理。
示例
var _ = require('underscore');
//Example 1. get Square of each number of array
var list = _.map([1, 2, 3], function(x) { return x*x });
console.log(list);
//Example 2. get squre of each number of object
list = _.map({one: 1, two: 2, three: 3}, function(value, key) { return value*value });
console.log(list);
將以上程式儲存到 tester.js 中。執行以下命令來執行此程式。
命令
\>node tester.js
輸出
[ 1, 4, 9 ] [ 1, 4, 9 ]
underscorejs_iterating_collection.htm
廣告