13 ms·
The json.org description of an "object" is: > An object is an unordered set of name/value pairs. But this site seems to be assuming that the key/value pairs a
by trevor-e 6y ago
The json.org description of an "object" is:
> An object is an unordered set of name/value pairs.
But this site seems to be assuming that the key/value pairs are parsed in their original ordering for everything to display properly. Is it safe to assume JavaScript will always parse the keys in order?
- ceejayoz 6y ago> Is it safe to assume JavaScript will always parse the keys in order? Technically, no. Functionally, probably yes. Any browser vendor that decides to muck with the current status quo would likely break enough code to be a non-starter.
- gpnt 6y agoThe website uses the basic render, that is just a start to make it easy to create a page quickly. But ideally, you create a custom render, that will not depend on the order.
- chrismorgan 6y agoECMAScript 2015 defines object iteration order as: number keys in ascending order, then other string keys in insertion order, then symbol keys in insertion order. See https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order https://stackoverflow.com/questions/5525795/does-javascript-.... And then JSON.parse is defined as doing the obvious and only sane thing, setting each property as it goes. Consequently, so long as your keys are not numeric, yes, JavaScript guarantees that it will all be in order. But that’s for JavaScript. It is incorrect to treat JSON objects as ordered, because various libraries in various languages will discard the order for various reasons (e.g. efficiency, DoS resistance), since it is defined as being not significant. If you care about the order of things, use an array instead.