Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change in order for .each() in firefox and chrome

Tags:

jquery

each

I have a web service that returns a JSON encoded array of data. I then use jQuery's .each() function to iterate through that array but in Firefox it iterates down while in Chrome it iterates up.

the data that come sback from the web service is:

{
  "data": {
    "610": {
      "id": "610",
      "url": "a url 1",
      "description": "XXX YYY",
      "toc": "0000-01-00",
      "active": "1"
    },
    "608": {
      "id": "608",
      "url": "a url 1",
      "description": "ytttgffrr",
      "toc": "0000-01-00",
      "active": "1"
    },
    "607": {
      "id": "607",
      "url": "a url  3",
      "description": "rtretert3",
      "toc": "0000-01-00",
      "active": "1"
    },
    "606": {
      "id": "606",
      "url": "a url 4",
      "description": "xxxwwww",
      "toc": "0000-01-00",
      "active": "1"
    },
    ...
  }
}

Firefox goes 610 -> 606 while chrome fors 606 -> 610.

Any ideas why and what I can do about it?

like image 515
Horaland Avatar asked Mar 23 '12 16:03

Horaland


1 Answers

Properties in objects have no inherent order. The ECMAScript specification doesn’t guarantee that for…in statements will traverse the object by its properties in alphabetical order. jQuery.each uses for…in under the hood when used on objects.

If the order is important to you, use an array instead of an object.

like image 93
Mathias Bynens Avatar answered Sep 30 '22 07:09

Mathias Bynens