我怎样才能填充数组[重复]

时间:2022-09-15 16:42:48

This question already has an answer here:

这个问题在这里已有答案:

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;
}