Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

for ... in statement and order in Chrome

Status
Not open for further replies.

Techi602

Programmer
Jul 23, 2009
1
CZ
Code:
var x;
var mycars = new Array();
mycars[3] = "Saab";
mycars[1] = "Volvo";
mycars[0] = "BMW";

for (x in mycars)
{
document.write(mycars[x] + "<br />");
}
all major browsers (IE, FF, O, S) sort items by the order when the item was added into array (Saab, VOlvo, BMW)

however Chrome sorts items by their index (BMW, Volvo, Saab)

Therefore there can be iregularities on website, while processing some JSON data and items are displayed in different order

how to fix this in Chrome? I have my data (array) in JSON and i need to display it (with index) in given order...
 
Hi

Techi602 said:
all major browsers (IE, FF, O, S) sort items by the order when the item was added into array (Saab, VOlvo, BMW)
Sometimes. Other times is differently. Personally I got :
[ul]
[li]BMW, Volvo, Saab : FireFox, Midori, Konqueror[/li]
[li]Saab, Volvo, BMW : SeaMonkey, Epiphany, Galeon, Opera[/li]
[/ul]
MDC said:
Iterates a specified variable over all the properties of an object, in arbitrary order.
( Mozilla Developer Center | for...in )

The conclusion : if the order matters, do not use [tt]for[/tt] .. [tt]in[/tt].

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top