Assuming a JavaScript array is posted in a form (using ajax/JQuery) is the order in the array guaranteed?
Valid question from @Quentin "Define 'posted in a form'"
$.ajax({ type: "POST", url: url, data: { foo: [{ name: "one" }, { name: "two" }] }, success: success, dataType: dataType });
Expanding on the question (because of the above), Is the array guaranteed to preserve the order on the server side?
It does not guarantee order The order of enumeration depends on the implementation which might differ between browsers. According to this article, all modern implementations iterate through object properties in the order in which they were defined.
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.
The underlying issue isn't because of map , which should preserve the order. Rather, user.jobs itself may be in a different order (in each test) since there isn't any explicit order by clause used. Without an explicit order by , you can't guarantee the order of the jobs even if you create them in a specific order.
Yes, theoretically, sets are not ordered, but mathematical abstractions, like sets, functions, numbers etc, are only tangentially related to similarly named objects we use in programming.
You can always refer to the ECMAScript standard when in doubts: Array Exotic Objects
Array is a special kind of object in the language that have additional semantic on how length property is handled respect to the properties that are integers from 0 to 2^32. In javascript an array can be sparse if there are missing values in the range of 0 to the length property excluded. Various array methods take this in consideration, for example forEach ignore missing values
What is a missing value?
The language tries to make arrays act as much as possible as to normal objects: you can add any property to it and even have objects that inherit from an array.
var fruits = ["Apple", "Banana"]; fruits.preferred = "Apple";
This kind of code don't pose any problem, but if you start to write:
fruits[100] = "Strawberry"; for(let i = 0; i < fruits.length; ++i) { ... }
100 is a property name in the range of [0, 2^32) so is an array element but at this point what fruits.length should be? It must be 101, otherwise the for loop will never get "Strawberry".
But at this point you have to accept that there is a range of element [2, 99] that were never defined: these are missing values.
Vice versa the same must be true when you modify the length property
var fruits = ["Apple", "Banana"]; fruits.length = 0;
Now a for loop will never get any element of the array: this is equivalent to emptying the fruits array.
It's also possible to increment the length, with the result of having missing values
So yes arrays are ordered as you can iterate all its elements in increasing order, but keep in mind that there can be missing values
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With