简体   繁体   English

将数组添加到另一个数组

[英]Add array to another array

I need to add some arrays to another array. 我需要将一些数组添加到另一个数组。

Suppose I have 2 nested loops: 假设我有2个嵌套循环:

arr1 = [];

for (i = 0; i < 3; i++) {
  for (j = 0; j < 3; j++) {
    arr1.push(i,j)
  }
}

I want arr1 to be 我希望arr1是

[[[0],[0]],[[0],[1]],[[0],[2]],[[1],[0]],...]

Instead I just get 相反,我只是得到

[0, 0, 0, 1, 0, 2, 1, 0, 1, 1, 1, 2, 2, 0, 2, 1, 2, 2]

Array.push appends each argument to the array, so this is expected behavior. Array.push将每个参数附加到数组,因此这是预期的行为。 To accomplish what you want you should call 要完成您想要的事情,您应该打电话给

arr1.push([[i], [j]]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM