Here is my code:
let rowValues = [];
let gridValues = [];
let testArray = [[1,1,1],[0,0,0],[1,1,1],[0,0,0],[1,1,1]];
let rows = 5;
let cols = 3;
for (let i = 0; i < rows; i++) {
rowValues.length = [];
for (let j = 0; j < cols; j++) {
if (testArray[i][j] === 0) {
rowValues.push(0);
} else if (testArray[i][j] === 1) {
rowValues.push(1);
}
}
gridValues.push(rowValues);
}
The problem is that the gridValues array updates with the rowValues array after the first iteration of the first for loop (once i = 1). It's like they become linked and when you update one the other follows suit. What is going on here??
All I want to have happen is build the rowValues array and then push it into the gridValues array to build a 2D array. But when rowValues is cleared at the start of the next iteration, gridValues is cleared too.