Associative array

Discussion in 'Javascript' started by Robert, Dec 9, 2004.

  1. Robert

    Robert Guest

    I am curious why some people feel that Javascript doesn't have
    associative arrays. I got these definitions of associative arrays via
    goggle:

    Arrays in which the indices may be numbers or strings, not just
    sequential integers in a fixed range.
    www.sunsite.ualberta.ca/Documentation/Gnu/gawk-3.1.0/html_chapter/gawk_20.html

    (n.) A collection of data (an array) where individual items can be
    indexed (accessed) by a string, rather than by an integer as is common
    in most programming languages.
    docs.sun.com/db/doc/805-4368/6j450e60a

    (n.) A collection of data (an array) where individual items may be
    indexed (accessed) by a string, rather than by an integer as is common
    in most programming languages.
    www.npac.syr.edu/nse/hpccgloss/hpccgloss.html

    Another name for a dictionary.
    academics.tjhsst.edu/compsci/thinkCS/chap19/node11.html


    My goggle search:
    http://www.google.com/search?hl=en&lr=&oi=defmore&q=define:Associative Array


    It seems to me that both javascript arrays and object meet these
    definitions.

    One problem with Javascript would be the extra indexes automatically
    created but you can check for them if you need too.
    Maybe I am missing something.

    Robert
     
    Robert, Dec 9, 2004
    #1
    1. Advertising

  2. Robert

    Randy Webb Guest

    Robert wrote:
    > I am curious why some people feel that Javascript doesn't have
    > associative arrays.


    Because Javascript, JScript nor ECMAScript have associative arrays.

    Pretty good reason don't you think?

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq
     
    Randy Webb, Dec 10, 2004
    #2
    1. Advertising

  3. On 9 Dec 2004 15:09:07 -0800, Robert <> wrote:

    > I am curious why some people feel that Javascript doesn't have
    > associative arrays.


    The behaviour has nothing to do with arrays. The misconception arises
    because "associative arrays" are introduced thus:

    var arr = new Array();

    arr['name'] = 'value';

    However, you can't apply any Array method to that object and expect it to
    act upon 'name' as it's not part of the array; it's part of the object
    itself. The same outcome is achieved with:

    arr.name = 'value';

    The main difference between the two approaches is that with dot notation,
    each token must conform to the rules of an identifier. With square bracket
    notation, the operand is any valid expression.

    I presume that many script authors are unaware that you can use square
    brackets to construct property names because the fact that this is what
    you're actually doing - accessing object properties - is never discussed.
    It's hidden among this illusion.

    The fact of the matter is: ECMAScript does not have them. To produce a
    robust system of storage you have to write at least *some* supporting
    code, which immediately draws attention to the fact that ECMAScript
    doesn't provide this functionality natively.

    If it helps in some way to think of associative arrays, fine. However,
    make sure you know what is really happening behind the scenes.

    [snip]

    > docs.sun.com/db/doc/805-4368/6j450e60a


    When posting URLs, please post them in full. Preferably surrounded by
    <URL:...>:

    <URL:http://docs.sun.com/db/doc/805-4368/6j450e60a>

    [snip]

    > It seems to me that both javascript arrays and object meet these
    > definitions.


    Loosely, perhaps. However, these terms are applied to languages which
    feature arrays that can be indexed by strings and numbers. You just aren't
    doing that with ECMAScript: an array is *just* an array.

    > One problem with Javascript would be the extra indexes automatically
    > created


    [Aside]
    It's interesting to see you refer to the "extra indexes". Either that's a
    Freudian slip, or potential proof of the misconception I mentioned earlier.
    [/Aside]

    > but you can check for them if you need too.


    How? You can't enumerate them, and you can never be sure of all of the
    properties and methods that will be present. It's best to avoid the issue
    entirely by modifying the name you use so that it will never clash with an
    existing property.

    [snip]

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, Dec 10, 2004
    #3
  4. "Michael Winter" <> wrote:

    > The behaviour has nothing to do with arrays. The misconception arises
    > because "associative arrays" are introduced thus:
    >
    > var arr = new Array();
    >
    > arr['name'] = 'value';


    Maybe one can show the misconception like this:

    var arr = new Array();

    (1) arr['name'] = 'value';

    Now it looks like one added a key/value pair to a hash.

    Array() has methods like pop() or push(), lets give the "hash datatype"
    some as well.

    (2) arr['keys'] = getkeys; // function defined elsewhere
    // The same as arr.keys = getkeys

    so now, arr.keys() should get the keys of the hash.

    But wait:

    arr['foo'] = bar is the way to add a key/value pair.

    So (2) should just do this and the hash should contain

    'name' = 'value'
    'keys' = getkeys

    But this is wrong as well.

    'keys' should not be a data of the hash. It should be a method like
    pop() for accessing the data of a hash.

    And yes, it is perfectly OK for a hash to hold functions.


    So this is a conflict it the alleged semantics of these
    assignment. The reason for the conflict is, that there is no native
    hash type in Javascript.

    Bye,
    Martin
     
    Martin Bialasinski, Dec 10, 2004
    #4
  5. Robert

    Guest

    Michael Winter wrote:
    > On 9 Dec 2004 15:09:07 -0800, Robert <> wrote:
    >
    > > I am curious why some people feel that Javascript doesn't have
    > > associative arrays.

    >
    > The behaviour has nothing to do with arrays. The misconception arises


    > because "associative arrays" are introduced thus:
    >

    <..>

    Then again associative arrays have nothing to do with arrays either,
    other than they evolved as an extension. Whereas arrays in the standard
    definition have only numerical indexes, associative arrays came into
    existence through removal of that restriction. Along with that
    extension, the concept of ordering based on indexes which is inherent
    in a standard array, is also removed.

    Even though not defined as such in the language specification, it
    doesn't preclude associative arrays from being considered to be an
    abstract data type. Therefore, the presence (or absence) of associative
    arrays can be shown through (failure to display) characteristic
    manifestation. As the purpose behind the abstraction is to separate
    external form and functionality from implementation, it is prudent to
    dispense with notions that have anything to do with a particular form
    of internal construct or implementation that may underlay.[1]

    As abstract data types, associative arrays have relatively simple form
    and functionality determinants. They are:
    ___

    Arbitrary keys associated with single values, along with four
    operations: add, change, remove, and lookup [2]
    ___

    It's pretty clear that, outside of a relatively small (possibly
    indeterminate) number of pre-defined key-values on instantiation,
    instances of Object conform quite well to the above.

    Regarding the pre-defined values, however, the 'add' operation doesn't
    convert an associative array into something which is not an associative
    array. Therefore this isn't an issue that has to do with existence of
    associative arrays, but rather has to do with expectations regarding
    initial content [3].

    So there are those who wish to say that Javascript does not have
    associative arrays, and others who say, at least conceptually and
    functionally, that it does. Both may be correct, but neither should be
    stated with out appropriate qualification.

    <..>

    > If it helps in some way to think of associative arrays, fine.
    > However, make sure you know what is really happening behind the

    scenes.

    <..>

    I think that's a remarkably fine and fair comment!

    Regards,

    .../rh

    [1] A hash imposes upon implementation, thus is an associative array,
    but not an ADT
    [2] Note the absence of enumeration
    [3] Object.prototype is an exception regarding transformation
    operations


    >
     
    , Dec 10, 2004
    #5
  6. Robert

    Robert Guest

    Randy Webb wrote:

    >
    > Because Javascript, JScript nor ECMAScript have associative arrays.



    You seem to be looking for specialized declare in Javascript for
    associative arrays. I am looking at the functionality of javascript.

    The definition of associative arrays doesn't require any specialize
    declaration.

    var a = [ ];
    a["abc"] = "data";
    alert(a["abc"]);

    // I do not see how this isn't fine associative array syntax either.
    var new = a.abc;

    The above is allowed in Javascript and meets the definition of
    associative arrays as found by google.

    Therefor for what I know about associtive arrays, Javascript has
    associative arrays (with the note about extra properties).

    ....

    I find it misleading to read posts in this forum that Javascript
    doesn't have associative arrays. These statements need to be qualified
    at a minimum.

    Robert
     
    Robert, Dec 10, 2004
    #6
  7. In article <>,
    Robert <> writes
    >I am curious why some people feel that Javascript doesn't have
    >associative arrays.


    One reason is that there are two meanings and no-one knows which is
    being used. 'Associative array' can describe a very general kind of
    storage unit, just as 'stack' can be a very general description.
    Alternatively, 'associative array' can be a particular data type
    supplied as part of a computer language. You can get some lovely
    never-ending arguments if people are using these different meanings.


    >I got these definitions of associative arrays via
    >goggle:
    >
    >Arrays in which the indices may be numbers or strings, not just
    >sequential integers in a fixed range.
    >www.sunsite.ualberta.ca/Documentation/Gnu/gawk-3.1.0/html_chapter/gawk_20.html
    >
    >(n.) A collection of data (an array) where individual items can be
    >indexed (accessed) by a string, rather than by an integer as is common
    >in most programming languages.
    >docs.sun.com/db/doc/805-4368/6j450e60a
    >
    >(n.) A collection of data (an array) where individual items may be
    >indexed (accessed) by a string, rather than by an integer as is common
    >in most programming languages.
    >www.npac.syr.edu/nse/hpccgloss/hpccgloss.html


    Back in the 60s when people were discussing hardware associative arrays
    the 'address', or key, or index, was just a bit pattern supplied by the
    user. The user might think it was ASCII characters or a floating point
    number or whatever, but the hardware couldn't care less.

    When an associative array is implemented in software the 'address' has
    to be a defined type so the compiler/interpreter knows how to allocate
    the right amount of storage when creating a cell, but it still isn't
    restricted to integers and strings.


    >Another name for a dictionary.
    >academics.tjhsst.edu/compsci/thinkCS/chap19/node11.html


    A dictionary is a particular kind of associative array. The 'address' is
    a word; the value is a chunk of text.

    But then people have used 'dictionary' in a more general way. 'Map' is
    another popular name.


    >My goggle search:
    >http://www.google.com/search?hl=en&lr=&oi=defmore&q=define:Associative Array
    >
    >
    >It seems to me that both javascript arrays and object meet these
    >definitions.
    >
    >One problem with Javascript would be the extra indexes automatically
    >created but you can check for them if you need too.
    >Maybe I am missing something.


    True. Both Object objects and Array objects have their good points and
    their bad points if your javascript application needs to implement an
    associative array.

    John
    --
    John Harris
     
    John G Harris, Dec 10, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nospam
    Replies:
    2
    Views:
    624
    Mohammd M. Hussain
    Jun 26, 2004
  2. desktop
    Replies:
    5
    Views:
    388
    James Kanze
    Jun 26, 2007
  3. Yvon Thoraval
    Replies:
    5
    Views:
    211
    Jason Creighton
    Sep 17, 2003
  4. VK
    Replies:
    47
    Views:
    553
    Thomas 'PointedEars' Lahn
    Jul 13, 2005
  5. VK
    Replies:
    36
    Views:
    658
    Martin Honnen
    Aug 3, 2005
Loading...

Share This Page