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?
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])
}