This question already has an answer here:
这个问题在这里已有答案:
- Partitioning in JavaScript [duplicate] 7 answers
- Split array into chunks 44 answers
在JavaScript中进行分区[重复] 7个答案
将数组拆分成块44个答案
I want to convert an array to an array of arrays.
我想将数组转换为数组数组。
For example if my array is:
例如,如果我的数组是:
a = [1,2,3,4,5,6]
I want:
b = [[1,2],[3,4],[5,6]]
How can I do this?
我怎样才能做到这一点?
1 个解决方案
#1
2
function Create2DArray(rows) {
var arr = [];
for (var i=0;i<rows;i++) {
arr[i] = [];
}
return arr;
}
#1
2
function Create2DArray(rows) {
var arr = [];
for (var i=0;i<rows;i++) {
arr[i] = [];
}
return arr;
}