I'm new to javascript and don't quite understand how the push() method works.
I've got two empty arrays, row and newData. And two pieces of codes with very different outputs:
for (i = 1; i <= 10 ; i++) {
row[0] = i;
newData.push(row);
}
results in newData == [10,10,10,...,10], which I find very surprising, and
for (i = 1; i <= 10 ; i++) {
newData.push(i);
}
results in newData == [1,2,3,...,8,9,10] which is the intended outcome.
But I do not understand why every iteration of the first loop seems to replace every element of newData with the last element, when the second loop works just as intended?
Thanks!