-3

How do I sort elements of {}?

for example

{id:'555555',name:'222222',tt:'66666'}

Become

{name:'222222',id:'555555',tt:'66666'}
jperelli
  • 6,631
  • 5
  • 49
  • 82
qiang
  • 7
  • 4

1 Answers1

0

As others have already explained, properties inside an object do not have any particular order. Yet, if you wish to access them in a particular order, you could sort Object.keys:

var obj = {zz: 'adsgasdh', id:'555555',name:'222222',tt:'66666'};

var sortedKeys = Object.keys(obj).sort();

console.log(sortedKeys);

sortedKeys.forEach(function(key) {
  console.log(key + ": " + obj[key]);
});
Nisarg Shah
  • 13,626
  • 5
  • 36
  • 53