Opened 11 years ago
Closed 11 years ago
#11034 closed bug (cantfix)
Reverse behavior of $.each() for objects in Chrome, Opera
Reported by: | Owned by: | ||
---|---|---|---|
Priority: | low | Milestone: | None |
Component: | core | Version: | 1.7.1 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
Hi, don't know, if its a bug of jQuery or of the browsers itself, but it seams that $.each() runs in reversed order on objects using Chrome or Opera (Firefox, Safari & IE8 seems to do it right). Here my shortend example:
<div id="container"></div> <script> var container = $('#container'); var data = { "3" : {"nr":1}, "2" : {"nr":2}, "1" : {"nr":3} }; $.each(data, function(k,e){ container.append('<span>'+e.nr+'</span>,'); }); </script>
Chrome & Opera displays: 3,2,1, The others: 1,2,3,
Using it with ajax and json will bring some strange behavior here and there. If an simple array with inner objects is used (like [{"nr":1},{"nr":2},{"nr":3}]) all will be right.
Change History (2)
comment:1 Changed 11 years ago by
Component: | unfiled → core |
---|---|
Owner: | set to [email protected]… |
Priority: | undecided → low |
Status: | new → pending |
comment:2 Changed 11 years ago by
Resolution: | → cantfix |
---|---|
Status: | pending → closed |
JavaScripts objects specify no particular order and implementations are not bound to preserve the order of object property fields. If you need numerically ordered behaviour, use an array.
http://es5.github.com/#x12.6.4
The mechanics and order of enumerating the properties (step 6.a in the first algorithm, step 7.a in the second) is not specified.
Thanks for taking the time to contribute to the jQuery project! Please provide a complete reduced test case on jsFiddle to help us assess your ticket.
Additionally, be sure to test against the jQuery Edge version to ensure the issue still exists. To get you started, use this boilerplate: http://jsfiddle.net/FrKyN/. Open the link and click to "Fork" (in the top menu) to get started.