Associative multidimensional arrays

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

    #1

    Associative multidimensional arrays

    Is there any way to associate name/value pairs during an array
    initialization? Like so:

    sType = "funFilter"
    filterTypeInfo = [];
    filterTypeInfo[sType] = new Array("type" : sType);

    I can do it using this:

    sType = "String"
    filterTypeInfo = [];
    filterTypeInfo[sType] = [];
    filterTypeInfo[sType]["type"] = sType;

    but that seems rather cludgy. I want to use array objects not Object
    objects as described here:



    Thanks,
    Derek Basch

  • McKirahan

    #2
    Re: Associative multidimensiona l arrays

    "Derek Basch" <dbasch@yahoo.c om> wrote in message
    news:1108770557 .411623.171880@ f14g2000cwb.goo glegroups.com.. .[color=blue]
    > Is there any way to associate name/value pairs during an array
    > initialization? Like so:
    >
    > sType = "funFilter"
    > filterTypeInfo = [];
    > filterTypeInfo[sType] = new Array("type" : sType);
    >
    > I can do it using this:
    >
    > sType = "String"
    > filterTypeInfo = [];
    > filterTypeInfo[sType] = [];
    > filterTypeInfo[sType]["type"] = sType;
    >
    > but that seems rather cludgy. I want to use array objects not Object
    > objects as described here:
    >
    > http://www.crockford.com/JSON/index.html
    >
    > Thanks,
    > Derek Basch
    >[/color]

    Have you looked at the Dictionary object?

    The Dictionary object stores information in name/value pairs.



    Comment

    • Lee

      #3
      Re: Associative multidimensiona l arrays

      Derek Basch said:[color=blue]
      >
      >Is there any way to associate name/value pairs during an array
      >initialization ? Like so:
      >
      >sType = "funFilter"
      >filterTypeIn fo = [];
      >filterTypeIn fo[sType] = new Array("type" : sType);
      >
      >I can do it using this:
      >
      >sType = "String"
      >filterTypeIn fo = [];
      >filterTypeIn fo[sType] = [];
      >filterTypeIn fo[sType]["type"] = sType;
      >
      >but that seems rather cludgy. I want to use array objects not Object
      >objects as described here:[/color]

      "associativ e arrays" in Javascript are *always* "Object objects".
      If the object happens to be an Array, that's just coincidental,
      because the Array attributes are ignored.

      Comment

      • mscir

        #4
        Re: Associative multidimensiona l arrays

        Derek Basch wrote:
        [color=blue]
        > but that seems rather cludgy. I want to use array objects not Object
        > objects<snip>[/color]

        As Derek said:



        So we have to use one of JavaScript's minor mysteries. In JavaScript,
        objects are also associative arrays (or hashes). That is, the property

        theStatus.Home

        can also be read or written by calling

        theStatus['Home']

        Comment

        • Richard Cornford

          #5
          Re: Associative multidimensiona l arrays

          mscir wrote:
          <snip>[color=blue]
          > So we have to use one of JavaScript's minor mysteries.[/color]

          Minor mystery or standard language construct?
          [color=blue]
          > In JavaScript,
          > objects are also associative arrays (or hashes).[/color]

          Objects are not necessarily either associative arrays or hashes (in the
          senses that the terms may be used in other languages). Objects may be
          implemented using associative arrays or hashes in whatever language the
          implementation is written in but the language specification is not
          interested in how its requirements are implemented. Objects are only
          required to be _dynamic_ associations of named properties with values.
          And indeed evidence suggests that the IE/JScript object implementation
          is a list of some sort.

          The important detail is that ECMAScript has no Array accessing syntax at
          all, only property accessors (one type of which resemble array accessing
          syntax from other languages). And that ECMAScript Array objects are just
          Objects that implement additional behaviour when properties are accessed
          (when the property names sufficiently resemble 32 bit unsigned integers)
          and have additional methods defined on a custom Array.prototype .

          Speaking of ECMAScript objects in terms of "associativ e arrays" and
          "hashes" tends to obscure the real nature of those objects and lead to
          misconceptions about what behaviour can be expected of ECMAScript
          objects.
          [color=blue]
          > That is, the property
          >
          > theStatus.Home
          >
          > can also be read or written by calling
          >
          > theStatus['Home'][/color]

          And even when the - theStatus - identifier is a reference to an object
          that is an Array object the array-ness of that object is irrelevant to
          the use of either of the above property accessors.

          Richard.


          Comment

          • Douglas Crockford

            #6
            Re: Associative multidimensiona l arrays

            > Is there any way to associate name/value pairs during an array[color=blue]
            > initialization? Like so:
            >
            > sType = "funFilter"
            > filterTypeInfo = [];
            > filterTypeInfo[sType] = new Array("type" : sType);[/color]

            filterTypeInfo = {}; /* Note the braces, not brackets */
            filterTypeInfo[sType] = {type : sType};

            or

            filterTypeInfor .funFilter = {type : sType};
            [color=blue]
            > I can do it using this:
            >
            > sType = "String"
            > filterTypeInfo = [];
            > filterTypeInfo[sType] = [];
            > filterTypeInfo[sType]["type"] = sType;
            >
            > but that seems rather cludgy.[/color]

            Right you are.
            [color=blue]
            > I want to use array objects not Object
            > objects as described here:
            >
            > http://www.crockford.com/JSON/index.html[/color]

            Why do you want to do it incorrectly?
            It is correct to use objects here, not arrays.

            Comment

            • rh

              #7
              Re: Associative multidimensiona l arrays

              Richard Cornford wrote:[color=blue]
              > mscir wrote:
              > <snip>[color=green]
              > > So we have to use one of JavaScript's minor mysteries.[/color]
              >
              > Minor mystery or standard language construct?
              >[color=green]
              > > In JavaScript,
              > > objects are also associative arrays (or hashes).[/color]
              >
              > Objects are not necessarily either associative arrays or hashes (in[/color]
              the[color=blue]
              > senses that the terms may be used in other languages). Objects may be
              > implemented using associative arrays or hashes in whatever language[/color]
              the[color=blue]
              > implementation is written in but the language specification is not
              > interested in how its requirements are implemented. Objects are only
              > required to be _dynamic_ associations of named properties with[/color]
              values.[color=blue]
              > And indeed evidence suggests that the IE/JScript object[/color]
              implementation[color=blue]
              > is a list of some sort.
              >
              > The important detail is that ECMAScript has no Array accessing syntax[/color]
              at[color=blue]
              > all, only property accessors (one type of which resemble array[/color]
              accessing[color=blue]
              > syntax from other languages). And that ECMAScript Array objects are[/color]
              just[color=blue]
              > Objects that implement additional behaviour when properties are[/color]
              accessed[color=blue]
              > (when the property names sufficiently resemble 32 bit unsigned[/color]
              integers)[color=blue]
              > and have additional methods defined on a custom Array.prototype .
              >
              > Speaking of ECMAScript objects in terms of "associativ e arrays" and
              > "hashes" tends to obscure the real nature of those objects and lead[/color]
              to[color=blue]
              > misconceptions about what behaviour can be expected of ECMAScript
              > objects.
              >[/color]

              As you correctly note, there is often a problem of misconception that
              arises when the term "associativ e array" chances into the vicinity of
              ECMAScript. However, in my view, the "real nature of objects" gets
              completely obscured when their relationship to "associativ e arrays" is
              tossed.

              The "real nature" of ECMAScript Object objects is that they are simply
              enhanced associative arrays. The enhancement comes through creation of
              a linkage (the prototype chain) of underlying associative arrays. In
              the case of the fundamental Object object, the chain is wonderfully
              short, and the ramifications upon key-based lookup entirely explainable
              and readily comprehensible.

              While you have provided all kinds of very valuable information in the
              above, it seems to me that anyone who is not fully familiar with
              ECMAScript objects characteristics would think that they are totally
              foreign and full of wonderful, mysterious construction, and of which
              the uninitiated are yet become fully cognizant. And that just
              constitutes a different form of obscuration, which I would quite truly
              think would not be your intent.

              Regards,

              ../rh

              Comment

              • Derek Basch

                #8
                Re: Associative multidimensiona l arrays

                Thanks for the thorough answer Richard. I had to read it about 20 times
                but I get it now.

                Given an array:

                family_names = new Array("Bob", "Judy");

                I can assign a property to it like this:

                family_names['daughter'] = 'Helga';

                because it is also an Object object.

                However, the 'daughter' property can't be accessed using 32 bit
                unsigned integers because it wasnt added to the Array object as an
                Array property but as an Object property. In other words:

                alert(family_na mes[0]);

                will return 'undefined'.

                alert(family_na mes['daughter']);

                will return 'Helga'.

                alert(family_na mes['0']);

                will return 'Bob'


                Further, this code:

                for (i in family_names)
                {
                alert(i);
                }

                returns '0', '1' and 'daughter'. So, apparently the 32 bit unsigned
                integers are properties of the object but the property value can't be
                accessed with the dotted notation that you could use to access the
                'daughter' property:

                family_names.0 = Error
                family_names.da ughter = Helga

                Interesting stuff. Feel free to correct any errors in my above logic.

                Comment

                • Richard Cornford

                  #9
                  Re: Associative multidimensiona l arrays

                  Derek Basch wrote:
                  <snip>[color=blue]
                  > Given an array:
                  >
                  > family_names = new Array("Bob", "Judy");
                  >
                  > I can assign a property to it like this:
                  >
                  > family_names['daughter'] = 'Helga';
                  >
                  > because it is also an Object object.
                  >
                  > However, the 'daughter' property can't be accessed
                  > using 32 bit unsigned integers because it wasnt added
                  > to the Array object as an Array property but as an
                  > Object property.[/color]

                  The "daughter" property of the Array object cannot be accessed as a 32
                  bit unsigned integer because the character sequence "daughter" in no way
                  resembles a 32 bit unsigned integer.
                  [color=blue]
                  > In other words:
                  >
                  > alert(family_na mes[0]);
                  >
                  > will return 'undefined'.[/color]

                  It will return "Bob". the number zero is type converted to the string
                  "0", which is the property name under which the string "Bob" is stored.
                  [color=blue]
                  > alert(family_na mes['daughter']);
                  >
                  > will return 'Helga'.
                  >
                  > alert(family_na mes['0']);
                  >
                  > will return 'Bob'[/color]

                  And the - length - property of the Array remains 2, regardless of the
                  additional enumerable property of the Array Object.
                  [color=blue]
                  > Further, this code:
                  >
                  > for (i in family_names)
                  > {
                  > alert(i);
                  > }
                  >
                  > returns '0', '1' and 'daughter'. So, apparently the
                  > 32 bit unsigned integers are properties of the object
                  > but the property value can't be accessed with the dotted
                  > notation that you could use to access the
                  > 'daughter' property:[/color]

                  Property names used in dot notation property accessors are required to
                  conform to the ECMAScript production rules for an Identifier; they may
                  not start with a decimal digit. Representations of 32 bit unsigned
                  integers start with a decimal digit and so cannot be identifiers.
                  Bracket notation does not place any limitations on the character
                  sequence used as the property name.
                  [color=blue]
                  > family_names.0 = Error[/color]

                  But it is a syntax error not a runtime error.
                  [color=blue]
                  > family_names.da ughter = Helga[/color]
                  <snip>

                  Richard.


                  Comment

                  Working...