The Docs say that Object.entries
returns an array of given objects enumerable property [key,value]
pairs . So yes its confusing if you look at this statement
const object3 = { 100: 'a', 2: 'b', 7: 'c' };
and end up getting ["2", "b"]
when you call Object.entries(object3)[0]
.
When you are doing this Object.entries(object3)[0]
, you are accessing a pair at the index of 0 returned by this function Object.entries(object)
. The order of this array has nothing to do with how you defined the object3
in the first place. The order according to the doc is the same as the provided by a
for...in
loop. I ran the for...in loop on the object and this is what i got as the order.
2,7,100
.
This is why you are getting ["2", "b"]
instead of ["100", "a"]
. As others have mentioned here , the order seems to be that way because 2<7<100.