My function accepts 2D array, then want to copy it, but i'm getting empty 2D array instead:
function getGeneration(cells, generations){
let cellsOfGen= [];
for (let i = 0; i < cells.length; i++) {
console.log(cells[i]) // normal output - [1,0,0], then [0,1,1], then [1,1,0]
cellsOfGen.push(cells[i]) // pushes empty array
}
console.log(cellsOfGen) // result - [ [], [], [] ]
}
let cells = [[1,0,0],[0,1,1],[1,1,0]]
console.log(getGeneration(cells, 1))
I've also tried to push every single item in nested arrays, and it worked:
function getGeneration(cells, generations){
let cellsOfGen= [];
for (let i = 0; i < cells.length; i++) {
console.log(cells[i]) // normal output - [1,0,0], then [0,1,1], then [1,1,0]
cellsOfGen.push(...cells[i])
}
console.log(cellsOfGen) // result - [ 1, 0, 0, 0, 1, 1, 1, 1, 0 ]
}
let cells = [[1,0,0],[0,1,1],[1,1,0]]
console.log(getGeneration(cells, 1))
I don't understand what's going on, help me if you know something please.