Re: JavaScript associative arrays not ordered?



Bart Van der Donck wrote:
Richard Cornford wrote:

[...]
Javascript does not have "associative arrays" [...]
Or "hashes".
[...]

"In JavaScript an object is a mapping from property names
to values -- that is, an associative array."

http://en.wikipedia.org/wiki/Associative_arrays#JavaScript

If wikipedia wish to define an "associative array" as "a mapping from
property names to values" (or more generally as 'a mapping of keys to
values') then that is their choice. So much of what associative arrays
actually are in practice is disregarded in that definition as to render
it trivial. Under that definition many things become "associative
arrays", some of which would be better never to be thought of as
"associative arrays".

Dictionary definitions of "array" tend to stress ordering in the
arrangement (and we have heard of the expectation of ordering (in some
sense) in "associative arrays" from the OP) yet javascript objects have
no ordering of their properties (except as a coincidental manifestation
of particular object implementations).

When an "associative array" is just created/instantiated in a language
that supports such it would be expected to be empty (or just have the
key/value pairs specified at creation), while the javascript Object is
never 'empty', and so cannot be assumed to not posses a value mapped to
an arbitrary key just because no such key/value pair has been assigned.

Many "associative array" in a language that supports such have some
(recoverable) notion of the number of key/value pairs assigned, while
javascript objects have no interest in the number or properties they
contain.

The practice of talking of either javascript Objects or Arrays as
"associative arrays" tends to introduce in the minds of the readers who
are familiar with actual associative arrays from other languages an set
of expectations that are not true (or not generally true) of javascript
Objects/Arrays. Inevitably false expectations about javascript will not
be satisfied by javascript, will tend to get in the way of an accurate
understanding of javascript, and will directly result in
issues/problems/bugs in code written to those expectations. The most
reasonable response to this situation is to state clearly that
javascript objects are not "associative arrays" and so allow the reader
to move on to the much more productive consideration of what a
javascript Object actually is.

Richard.


.



Relevant Pages

  • Re: Using AJAX/JSON and performance issues with eval()
    ... evalevaluates its string argument as an ECMAScript ... JS arrays are implemented as objects, ... multiple HTML tables automatically in the background) and really want it ... Prototype.js was written by people who don't know javascript for people ...
    (comp.lang.javascript)
  • Re: Math.random
    ... But, as the seed is exposed, randoms can be ... Using just ordinary JavaScript ... arithmetic has to be done with arrays of digits. ... as one might reasonably have hoped, and also is a security leak. ...
    (comp.lang.javascript)
  • Object instead of array? Any problems?
    ... Javascript isn't my specialty, ... TONS of arrays, and I'm using vbscript to actually build them into the ... lol - it's a mess. ...
    (comp.lang.javascript)
  • Re: Dependent dropdowns
    ... > Looking for a simple javascript solution to this and it's annoying me ... of OPTION tags from arbitrary lists of data available on the server than to ... build JS arrays, and the demands on the client are smaller. ...
    (microsoft.public.scripting.jscript)
  • Re: how matrices elements mapping to contents of structure
    ... how can i read any number from matrices A, and then, mapping the contents of G to that location, for example: ... As you see, what I've done is, through a loop, replaced the original A ... matrix with the corresponding arrays in G to those values in A. Is ...
    (comp.soft-sys.matlab)