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

258
Views
Javascript Empty filling 2D array using Array.fill() not working

Array.fill() doesn't work for 2D arrays.

I was solving an algorithm where I need to rotate a matrix to 90degrees. Using map() and Array().fill() completely changes the actual result. I am confused what could be the reason for that

function rotate90(matrix) {
    const len = matrix.length
    const rotate = matrix.map(_ => [])
    const rotate2 = new Array(len).fill([])

    console.log('rotate', rotate);
    console.log('rotate2', rotate2);

    for (let i = 0; i < len; i++) {
        for (let j = 0; j < len; j++) {
            rotate[j][len - 1 - i] = matrix[i][j]
            rotate2[j][len - 1 - i] = matrix[i][j]
        }
    }

    console.log("using map()")
    for (let i = 0; i < len; i++) {
        console.log(rotate[i])
    }
    console.log("using new Array()")
    for (let i = 0; i < len; i++) {
        console.log(rotate2[i])
    }
    return rotate
}

Result: enter image description here

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!