P
P
pr0fedt2011-11-08 14:51:44
JavaScript
pr0fedt, 2011-11-08 14:51:44

For..in issue in javascript on IE9

Good afternoon,
Let's say there is the following JSON: When used in IE 9, it will display IDs in sorted order Question: how to get rid of this (because in normal browsers and even IE7 everything works fine, namely the order is preserved)?
a = {"10173":{},"10178":{},"10180":{},"10177":{}};

for(var id in a)
{
alert(id);
}

Answer the question

In order to leave comments, you need to log in

5 answer(s)
V
Vitaly Zheltyakov, 2011-11-08
@pr0fedt

This is observed not only in IE, but also in Opera.
Rebuild your logic - add an additional field responsible for the output order.

K
Konstantin Kitmanov, 2011-11-08
@k12th

This is not an array (list), but a hash (dictionary). Therefore, the order of the keys is not guaranteed. Even if the order is respected in some implementations, this is not an indicator.
So, if the order is critical, you have to sort.

O
Oleg Yakovenko, 2011-11-08
@Apx

So you don't want to?
var a = [{id:"10173"},{id:"10178"},{id:"10180"},{id:"10177"}];
for(var ind in a)
{
var obj = a[ind];
alert(obj.id);
}

P
pr0fedt, 2011-11-08
@pr0fedt

it was just that the whole point was that the array was transferred from the server in this form by ajax, since there was a lot of use where I didn’t really want to change the structure. I just decided to additionally pass sorting.

S
spmbt, 2011-12-10
@spmbt

I solved this problem with hash sorting in IE by putting a letter in front of the key (you can use any other character). Then it's not sorted. Interestingly, the problem is only with IE9, Opera and Chrome. Safari and FF are fine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question