For example the following
var data = {
'States': ['NSW', 'VIC'],
'Countries': ['GBR', 'AUS'],
'Capitals': ['SYD', 'MEL']
}
for (var item in data) {
console.log(item);
}
prints
States
Countries
Capitals
Is there a way to sort alphabetically so that it prints
Capitals
Countries
States
Not within the object itself: the property collection of an object is unordered.
One thing you could do is use
Object.keys(), and sort the Array, then iterate it.Patches (implementations) for browsers that do not support ECMAScript 5th edition:
Object.keys
Array.forEach