Answer the question
In order to leave comments, you need to log in
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
This is observed not only in IE, but also in Opera.
Rebuild your logic - add an additional field responsible for the output order.
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.
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);
}
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question