Mi función acepta una matriz 2D, luego quiero copiarla, pero obtengo una matriz 2D vacía en su lugar:
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))También intenté empujar cada elemento en matrices anidadas, y funcionó:
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))No entiendo lo que está pasando, ayúdame si sabes algo por favor.