Associative array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert

    Associative array

    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.


    (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.


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


    My goggle search:



    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

  • Randy Webb

    #2
    Re: Associative array

    Robert wrote:[color=blue]
    > I am curious why some people feel that Javascript doesn't have
    > associative arrays.[/color]

    Because Javascript, JScript nor ECMAScript have associative arrays.

    Pretty good reason don't you think?

    --
    Randy
    comp.lang.javas cript FAQ - http://jibbering.com/faq

    Comment

    • Michael Winter

      #3
      Re: Associative array

      On 9 Dec 2004 15:09:07 -0800, Robert <rccharles@my-deja.com> wrote:
      [color=blue]
      > I am curious why some people feel that Javascript doesn't have
      > associative arrays.[/color]

      The behaviour has nothing to do with arrays. The misconception arises
      because "associativ e 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]
      [color=blue]
      > docs.sun.com/db/doc/805-4368/6j450e60a[/color]

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

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

      [snip]
      [color=blue]
      > It seems to me that both javascript arrays and object meet these
      > definitions.[/color]

      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.
      [color=blue]
      > One problem with Javascript would be the extra indexes automatically
      > created[/color]

      [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]
      [color=blue]
      > but you can check for them if you need too.[/color]

      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.

      Comment

      • Martin Bialasinski

        #4
        Re: Associative array

        "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote:
        [color=blue]
        > The behaviour has nothing to do with arrays. The misconception arises
        > because "associativ e arrays" are introduced thus:
        >
        > var arr = new Array();
        >
        > arr['name'] = 'value';[/color]

        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

        Comment

        • John G Harris

          #5
          Re: Associative array

          In article <1102633747.076 556.123190@z14g 2000cwz.googleg roups.com>,
          Robert <rccharles@my-deja.com> writes[color=blue]
          >I am curious why some people feel that Javascript doesn't have
          >associative arrays.[/color]

          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.

          [color=blue]
          >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.
          >https://www.sunsite.ualberta.ca/Docu...r/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[/color]

          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.

          [color=blue]
          >Another name for a dictionary.
          >academics.tjhs st.edu/compsci/thinkCS/chap19/node11.html[/color]

          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.

          [color=blue]
          >My goggle search:
          >http://www.google.com/search?hl=en&l...ociative+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.[/color]

          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

          Comment

          Working...