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

172
Views
The Object.fromEntires() change the sequence of the values in an array when key is numerical-- Javascript

I have a array:

arr = [ ['1', 'a'], ['12', 'b'], ['2', 'c'] ];

I'm using Object.fromEntries() to convert the array into objects with key/value pair. But the problem is Object.fromEntries() seems to changing the sequence of my array based on the key, eg: I want my above array to be converted into object with the same sequnce as shown in the code:

expected o/p:

{1: 'a', 12: 'b', 2: 'c'}

Actual o/p from the fromEntires()

{1: 'a', 2: 'c', 12: 'b'}

any idea why does the function do that and if there is a way to avoid this and render the same sequcne as provided in the array?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

The order of properties in JavaScript objects is not guaranteed to be the insertion order. In particular, keys that parse as integers (such as in your case) will not respect insertion order.

If you need to guarantee order, try using a Map instead:

let arr = [ ['1', 'a'], ['12', 'b'], ['2', 'c'] ];
const map = new Map();
for(let value of arr){
 map.set(value[0], value[1])
}
about 4 years ago · Santiago Gelvez Report
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!