Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

95
Vistas
Can't copy 2D Array in JavaSctipt

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.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda