0

I create an object, and then pass on all his keys. It turns out they are ordered! How to avoid it? Is it real?

var obj = { "A": 1, "C": 2, "B": 3 };
for (var key in obj)
{
  //receiving in turn A, B and C instead of A, C, B
}
  • 1
    visit->http://stackoverflow.com/questions/3186770/chrome-re-ordering-object-keys-if-numerics-is-that-normal-expected – Sagar Naliyapara Apr 02 '15 at 13:36
  • The order of keys in not specified within an object (as per the javascript specification), don't ever rely on it. You will find different behaviours in different browsers, maybe even between different versions of the same browser. – doldt Apr 02 '15 at 13:37
  • I am very disappointed =( Thanks for the reply. – Дмитрий Чистик Apr 02 '15 at 13:40
  • Here is Jsfiddle and it displays- A,C,B- https://jsfiddle.net/abvzLc5d/ – WonderHeart Apr 02 '15 at 13:50
  • I think that was going on in the selected browser. For example, if you specify the keys "1", "3", "2", the result will be, as I described above [jsfiddle](http://jsfiddle.net/abvzLc5d/1/) – Дмитрий Чистик Apr 02 '15 at 14:03
  • If the Keys are integers, then please refer to the link @SagarNaliyapara provided. If the Key's are having letter in the beginning, then it displays the way you expect. – WonderHeart Apr 02 '15 at 14:17

0 Answers0