Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do javascript arrays and objects have a set order?

This question is vital to one of my current projects involving creating HTML tables from JSON objects. I was thinking I could create functions that would sort my Arrays/Objects before rendering them as HTML. My only concern is that order doesn't matter in JavaScript, and if I were to create a new Array with the same data (in a different order) as another Array they would end up identical.

I can't think of a fast way to test this, so I'm asking here.

like image 735
Hubro Avatar asked Jun 11 '11 15:06

Hubro


People also ask

Are JavaScript arrays in order?

JavaScript arrays are an ordered collection that can hold data of any type. Arrays are created with square brackets [...] and allow duplicate elements. In JavaScript, we can sort the elements of an array with the built-in method called, sort() .

Are sets in JavaScript ordered?

A set is not an ordered abstract data structure. A Set however always has the same iteration order - element insertion order [1], so when you iterate it (by an iterating method, by calling Symbol. iterator , or by a for.. of loop) you can always expect that. You can always convert the set to an array and sort that.

Do JavaScript objects maintain order?

YES (but not always insertion order). Most Browsers iterate object properties as: Integer keys in ascending order (and strings like "1" that parse as ints) String keys, in insertion order (ES2015 guarantees this and all browsers comply) Symbol names, in insertion order (ES2015 guarantees this and all browsers comply)

Does the order of elements in an array matter in JavaScript?

If you enumerate ( for in ) or for loop over an array it will be ordered. So if you create two arrays with the items in different orders then they are not identical. Enumerations over objects are ordered based on whatever the browser feels like.


3 Answers

Others have answered on arrays, so I just wanted to provide some info on objects.

On that, the standard is non-existent, but it's de facto definition is that an object will enumerate in insertion order, with the exception that numbers are placed in ascending order and enumerated first. I say typically, but this behavior is nowhere near standardized (nor do frameworks like jQuery standardize it, AFAIK).

You can test browsers using this jsFiddle:

http://jsfiddle.net/7cCpu/4/

The object {"foo":"bar", "bar":"foo", "baz":"baz", "3":3, "2":2, "1":1} enumerates as follows:

foo, bar, baz, 3, 2, 1 // insertion order

1, 2, 3, foo, bar, baz // Chrome enumeration
1, 2, 3, foo, bar, baz // Opera
1, 2, 3, foo, bar, baz // IE9
foo, bar, baz, 3, 2, 1 // Firefox (!!!)

I don't have Safari installed, but I assume it's the same as Chrome. In any case, the point is that you can make assumptions -- it's not random -- but it's probably a better idea to use an array if you depend on exact enumeration.

Even nastier is what duri pointed out above, where deleting and replacing the value for a key alters things further. Watch what happens when I delete bar and do Object.bar = "foo" then enumerate:

1, 2, 3, foo, baz, bar // Chrome enumeration
1, 2, 3, foo, baz, bar // Opera
1, 2, 3, foo, bar, baz // IE9 (!!!)
foo, baz, 3, 2, 1, bar // Firefox (!!!)
like image 196
brymck Avatar answered Oct 23 '22 01:10

brymck


Arrays are enumerated by (numeric) index.

Objects aren't - the order isn't defined, indeed the Javascript standards explicity state that the order is undefined. For more detail see Does JavaScript Guarantee Object Property Order?

like image 5
Alnitak Avatar answered Oct 23 '22 00:10

Alnitak


Good example when order of object properties differs is this code:

var o = { foo: 1, bar: 2, baz: 3 };
delete o.bar;
o.bar = 2;
for (var i in o)    {
    document.write(o[i]);
}

This displays 132 in Firefox, but 123 in Internet Explorer.

like image 2
duri Avatar answered Oct 23 '22 01:10

duri