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

142
Visualizações
wired behaviors with javascript arrays

Though I know what results it ends with but it's a really trap, kinda of anti-intuition of normal understanding of how these array API works.

Take a look: Case 1

const x = new Array(2).fill([]);
x[0].push("item 1")
x[1].push("item 2")

intention: initial an array with two empty sub-array, and push two different items into each.

expectation: x=[["item 1"], ["item 2"]]

reality: x=[["item 1", "item 2"], ["item 1", "item 2"]]

seems like, the "fill" function takes the same object of "[]", wired, isn't it?

Case 2

const x = new Array(3).map(
          (_, i) => `col_${i}`,
        );

intention: create an array of given length with default value of "col_[index]" for each item.

expectation: x=["col_0", "col_1", "col_2"]

reality: x=[null, null, null]

So it looks like "new Array()" is just has a length property but not really you can iterate over it.

Questions:

  1. It is really like a puzzle, anybody have insights into why it is designed in this way? Or just - well it is javascript, let's do whatever we like... :(
  2. For the above two intentions - what are the simplest ways you will make?
about 4 years ago · Juan Pablo Isaza
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