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

135
Views
How to create an array filled any number of empty arrays in JavaScript?

Per the title, I'm trying to create an array that is filled with any number of empty arrays in JavaScript.

As an example, let's say I wanted to create an array filled with 9 empty arrays. I could do something like this, which works, but looks quite bad:

let myArray = [[], [], [], [], [], [], [], [], []];

I tried using the fill method, but if I push to any of the empty arrays, the other arrays get filled as well:

let myArray = Array(9).fill([]);

myArray[0].push(1);
// Output = [[1], [1], [1], [1], [1], [1], [1], [1], [1]];

Is there a better way to do this? Perhaps a nice one-liner?

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

0

Fill it with Array.fill, then map over each item:

let myArray = Array(9).fill().map(e => [])
myArray[0].push(1);

console.log(myArray)

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!