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

137
Views
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
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!