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

138
Visualizações
Looping though array and putting values as object keys in reverse order

So I have two scenarios, one where the array looks like ["20, "21", "22", "23", "24"] and I'm able to successfully loop through the array to create an object with each array value is the object key (i.e. {"20": [],"21": [],"22": [],"23": [],"24": []}). This is working as intended. The problem occurs when I have an array like ["24", "23", "22", "21", "20"], and when this is looped over, it creates an object exactly like the first scenario {"20": [],"21": [],"22": [],"23": [],"24": []}, when I need it to be {"24": [],"23": [],"22": [],"21": [],"20": []}. I have no clue what could be going on as I've read that javascript objects maintain their order

let numberObject = {};

["20", "21", "22", "23", "24"].forEach((date) => {
numberObject[date] = [];
});

Output: {"20": [],"21": [],"22": [],"23": [],"24": []}

let numberObject = {};

["24", "23", "22", "21", "20"].forEach((date) => {
numberObject[date] = [];
});

Output: {"20": [],"21": [],"22": [],"23": [],"24": []}
Expected output: {"24": [],"23": [],"22": [],"21": [],"20": []}

Any idea why the expected output isn't occurring?

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