Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda