Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!