Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

139
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda