Ticket #11034 (closed bug: cantfix)
Reverse behavior of $.each() for objects in Chrome, Opera
| Reported by: | loewel@… | Owned by: | loewel@… |
|---|---|---|---|
| Priority: | low | Milestone: | None |
| Component: | core | Version: | 1.7.1 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
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
comment:1 Changed 18 months ago by timmywil
- Owner set to loewel@…
- Priority changed from undecided to low
- Status changed from new to pending
- Component changed from unfiled to core
comment:2 Changed 18 months ago by rwaldron
- Status changed from pending to closed
- Resolution set to cantfix
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.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

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.