Evgenij Reznik
Evgenij Reznik

Reputation: 18614

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]

Upvotes: 0

Views: 131

Answers (1)

szym
szym

Reputation: 5846

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]]);

Upvotes: 8

Related Questions