Bug Tracker

Opened 7 years ago

Closed 7 years ago

#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:
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 7 years ago by timmywil

Component: unfiledcore
Owner: set to loewel@…
Priority: undecidedlow
Status: newpending

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.

comment:2 Changed 7 years ago by Rick Waldron

Resolution: cantfix
Status: pendingclosed

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.

Note: See TracTickets for help on using tickets.