- 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 - 塊方法
語法
_.chunk(array, length)
塊方法使用 length 引數從陣列生成多個數組。生成的陣列可以為給定長度的最大大小。
示例
var _ = require('underscore');
var list1 = [ 'Sam', 'Joe','Julie' ]
var list2 = [ 1, 2, 3, 4, 5, 6 ]
//Example 1: create arrays using list1
result = _.chunk(_.shuffle(list1), 2);
console.log(result)
//Example 2: create arrays using list2
result = _.chunk(list2, 2);
console.log(result)
將以上程式儲存在 tester.js 中。執行以下命令執行此程式。
命令
\>node tester.js
輸出
[ [ 'Julie', 'Joe' ], [ 'Sam' ] ] [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
underscorejs_processing_array.htm
推薦廣告