Dear Lazzie
|
2007-02-04Order in the codeA reader from San Mateo writes:
Gentle reader,
An object is just a hash table, and there is no implicit order of elements of a hash table (Java has If you want to iterate over an array’s entries in order, use
If you want to iterate over an object’s keys in a particular order, you have to pull the keys out into an array, sort that array, and then iterate over that array:
The fact that any particular runtime iterates over object keys in any particular order reveals an implementation detail of their hashes, but if you write your code to depend on that you are setting yourself up for a fall. Most Javascript runtimes just happen to use particularly trivial hash implementations that give you the illusion the keys are iterated in order. See also the yellow box caution here. Post a comment |
|
Thanks for signing in, . Now you can comment. (sign out)
(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)