我有一个像这样的数组:
arr = [ [[x,x],[x,x]], [[x,x],[x,x],[x,x]], [[x,x]] ]
Run Code Online (Sandbox Code Playgroud)
我想把它变成一个像这样的数组:
arr = [ [x,x],[x,x] , [x,x],[x,x],[x,x], [x,x] ]
Run Code Online (Sandbox Code Playgroud)
所以我尝试过这个:
for (var i=1; i< arr.length; i++){ arr[0].concat(arr[i]); }
Run Code Online (Sandbox Code Playgroud)
但它不起作用。我怎样才能“合并”这个中间级别的数组?
对于 ES6,你可以spread syntax使用concat()
var arr = [ [['x','x'],['x','x']], [['x','x'],['x','x'],['x','x']], [['x','x']] ]
var merged = [].concat(...arr)
console.log(JSON.stringify(merged))Run Code Online (Sandbox Code Playgroud)
concat()对于旧版本的 ecmascript,可以使用和完成相同的操作apply()。
var arr = [ [['x','x'],['x','x']], [['x','x'],['x','x'],['x','x']], [['x','x']] ]
var merged = [].concat.apply([], arr)
console.log(JSON.stringify(merged))Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1592 次 |
| 最近记录: |