0

May be this is the normal behavior but I am little bit confused. So this is my scenario

var myObj = { 10: "2", 20: "3", 30: "4", min: "1", max: "5" };
keysSorted = Object.keys(myObj).sort((a,b) => myObj[a]-myObj[b]);
var obj = {};
keysSorted.forEach(function(item){
    console.log(item , myObj[item]);
    obj[item] = myObj[item];
});
console.log(obj);

Output:

 min 1
 10 2
 20 3
 30 4
 max 5
{10: "2", 20: "3", 30: "4", min: "1", max: "5"}

What I thought It will be

Output:

 min 1
 10 2
 20 3
 30 4
 max 5
{min: "1", 10: "2", 20: "3", 30: "4", max: "5"}

So my question is why the object creation order not sequential inside forEach?

Always Sunny
  • 29,081
  • 6
  • 43
  • 74

0 Answers0