- 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 - flatten 方法
語法
_.flatten(array, [shallow])
flatten 方法展平巢狀陣列,巢狀長度可為任意長度。如果將 shallow 傳遞為 true,則陣列僅展平到第一層。
示例
var _ = require('underscore');
var list = [1, [2], [4], 5, [[6]]]
//Example 1: flatten list
result = _.flatten(list);
console.log(result)
//Example 2: flatten list to first level only
result = _.flatten(list, true);
console.log(result)
將上述程式儲存在 tester.js 中。執行以下命令執行此程式。
命令
\>node tester.js
輸出
[ 1, 2, 4, 5, 6 ] [ 1, 2, 4, 5, [ 6 ] ]
underscorejs_processing_array.htm
廣告