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

116
Views
How to change an element in a multidimensional array in Javascript

I've created a mutidimensional array like this: let array = new Array(7).fill(Array(7).fill());

Then I tried to change array[0][0], like this: array[0][0] = 1;

But somehow, this changes the first element of every inner array. This is the output I've got:

Array(7)
0: (7) [1, undefined, undefined, undefined, undefined, undefined, undefined]
1: (7) [1, undefined, undefined, undefined, undefined, undefined, undefined]
2: (7) [1, undefined, undefined, undefined, undefined, undefined, undefined]
3: (7) [1, undefined, undefined, undefined, undefined, undefined, undefined]
4: (7) [1, undefined, undefined, undefined, undefined, undefined, undefined]
5: (7) [1, undefined, undefined, undefined, undefined, undefined, undefined]
6: (7) [1, undefined, undefined, undefined, undefined, undefined, undefined]

I can't imagine what I did wrong. Any ideas?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

const newArr = (a, b) => {
  return Array.from({length: a}, (_, i) => {
    return Array.from({length: b}, (_, j) => {
      return [i, j]
    })
  })
}
console.log(newArr(4, 3));
about 4 years ago · Juan Pablo Isaza Report

0

You are using the same array reference when filling with the array. Instead use a .map() to fill instead.

const arr2D = Array(7).fill(0).map(()=> Array(7).fill(0));
about 4 years ago · Juan Pablo Isaza Report
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!