Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

94
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda