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

97
Views
Cannot properly change the value of a Two-dimensional array

i created a Two-dimensional array and tried to change the value of an element in it like this:

    let testMap=new Array(3).fill(new Array(4).fill(0));
    console.log("begin",testMap);
    testMap[0][0]=1;
    console.log("end",testMap);

i thought the result would be testMap[0][0]=1 and others didn't change,but its result was(at Leetcode):

begin [ [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ] ]
end [ [ 1, 0, 0, 0 ], [ 1, 0, 0, 0 ], [ 1, 0, 0, 0 ] ]

what's more when it ran at my Vscode, its result was(using Chrome):

begin [ [ 1, 0, 0, 0 ], [ 1, 0, 0, 0 ], [ 1, 0, 0, 0 ] ]
end [ [ 1, 0, 0, 0 ], [ 1, 0, 0, 0 ], [ 1, 0, 0, 0 ] ]

Why is the entire column of elements modified and their results different?i wonder why it happened and it really made me puzzled.if i want to avoid it,which method is recommended?thanks

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!