-3

How do I sort elements of {}?

for example

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

Become

{name:'222222',id:'555555',tt:'66666'}
jperelli
  • 6,988
  • 5
  • 50
  • 85
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
  • 14,151
  • 6
  • 34
  • 55