简体   繁体   中英

Add array to another array

I need to add some arrays to another array.

Suppose I have 2 nested loops:

arr1 = [];

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

I want arr1 to be

[[[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. To accomplish what you want you should call

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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