How to iterate javascript object properties in the order they were written

No. JavaScript Object properties have no inherent order. It is total luck what order a for...in loop operates.

If you want order you’ll have to use an array instead:

var map= [[23, 'AA'], [12, 'BB']];
for (var i= 0; i<map.length; i++)
    document.write('Key '+map[i][0]+', value: '+map[i][1]);

Leave a Comment