Can I create a 2-D associative array - if so how?

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

    Can I create a 2-D associative array - if so how?

    Suppose I wanted to create an array that was associative in 2
    dimensions. The rows are associated with numbers. The columns with
    words.

    For instance for 3 columns. This array will have 20 rows of three
    columns and the words that I want to associate with the columns are
    'Small', 'Medium' and 'Large'. All the array elements are positive
    integers. What's the easiest way to do this?

    The first 3 columns are shown below for the array that is associative
    by row:

    sizes = {1:[16, 17, 19], 2:[16, 18, 23], 3:[16, 18, 25] }

    This is how I can do it for columns:

    function sizes(Small, Medium, Large) {
    this.Small = Small;
    this.Medium = Medium
    this.Large = Large;
    }

    r1 = new sizes(16, 19, 17);
    r2 = new sizes(16, 23, 18);
    r3 = new sizes(16, 25, 18);

    The problem with that is that the rows are now indexed via a
    zero-based index instead of the code I wanted to use (as in the first
    definition of sizes above) - accessed via an index starting at 1.

    Is there a way I could create an associative array to refer to data
    item 25 in the array as:

    sizes[3][Large]

    PS: As always this is an illustrative example.

  • Michael Winter

    #2
    Re: Can I create a 2-D associative array - if so how?

    On Fri, 24 Sep 2004 22:09:44 +0100, mark4asp
    <mark4asp#kills pam#@ntlworld.c om> wrote:
    [color=blue]
    > Suppose I wanted to create an array that was associative in 2
    > dimensions. The rows are associated with numbers. The columns with words.[/color]

    Just to correct any misconceptions. ..

    There is no such thing as an associative array in Javascript. You can
    emulate them with the properties of an object, but they are not the same
    thing.
    [color=blue]
    > For instance for 3 columns. This array will have 20 rows of three
    > columns and the words that I want to associate with the columns are
    > 'Small', 'Medium' and 'Large'. All the array elements are positive
    > integers. What's the easiest way to do this?
    >
    > The first 3 columns are shown below for the array that is associative by
    > row:
    >
    > sizes = {1:[16, 17, 19], 2:[16, 18, 23], 3:[16, 18, 25] }[/color]

    sizes = {
    small : [16, 17, 19],
    medium : [16, 18, 23],
    large : [16, 18, 25]
    };

    sizes['large'][1] // 18
    sizes.medium[0] // 16

    var col = 'small';
    sizes[col][2] // 19

    [snip]

    Hope that helps,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Can I create a 2-D associative array - if so how?

      mark4asp <mark4asp#kills pam#@ntlworld.c om> writes:
      [color=blue]
      > Suppose I wanted to create an array that was associative in 2
      > dimensions. The rows are associated with numbers. The columns with
      > words.[/color]
      ....[color=blue]
      > sizes = {1:[16, 17, 19], 2:[16, 18, 23], 3:[16, 18, 25] }[/color]
      ....[color=blue]
      > function sizes(Small, Medium, Large) {[/color]

      I would capitalize "sizes" to show that it is used as a constructor
      function.
      [color=blue]
      > this.Small = Small;
      > this.Medium = Medium
      > this.Large = Large;
      > }
      >[/color]
      ....[color=blue]
      > The problem with that is that the rows are now indexed via a
      > zero-based index instead of the code I wanted to use (as in the first
      > definition of sizes above) - accessed via an index starting at 1.[/color]

      Try
      var sizes = {1: new Sizes(16, 19, 17),
      2: new Sizes(16, 23, 18),
      3: new Sizes(16, 25, 18)}
      [color=blue]
      > Is there a way I could create an associative array to refer to data
      > item 25 in the array as:
      >
      > sizes[3][Large][/color]

      'ere you go.

      When you don't use the "arrayness" of the first coordinate (which basically
      means that you don't use the "length" property or any method on it that
      comes from Array.prototype ), then you can just as well use a plain object.

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Can I create a 2-D associative array - if so how?

        Lasse Reichstein Nielsen <lrn@hotpop.com > writes:
        [color=blue][color=green]
        >> sizes[3][Large][/color]
        >
        > 'ere you go.[/color]

        Ack. That should be
        sizes[3]['Large']
        or
        sizes[3].Large

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • mark4asp

          #5
          Re: Can I create a 2-D associative array - if so how?

          On Sat, 25 Sep 2004 10:48:15 GMT, "Michael Winter"
          <M.Winter@bluey onder.co.invali d> wrote:
          [color=blue]
          >On Fri, 24 Sep 2004 22:09:44 +0100, mark4asp
          ><mark4asp#kill spam#@ntlworld. com> wrote:
          >[color=green]
          >> Suppose I wanted to create an array that was associative in 2
          >> dimensions. The rows are associated with numbers. The columns with words.[/color]
          >
          >Just to correct any misconceptions. ..
          >
          >There is no such thing as an associative array in Javascript. You can
          >emulate them with the properties of an object, but they are not the same
          >thing.
          >[color=green]
          >> For instance for 3 columns. This array will have 20 rows of three
          >> columns and the words that I want to associate with the columns are
          >> 'Small', 'Medium' and 'Large'. All the array elements are positive
          >> integers. What's the easiest way to do this?
          >>
          >> The first 3 columns are shown below for the array that is associative by
          >> row:
          >>
          >> sizes = {1:[16, 17, 19], 2:[16, 18, 23], 3:[16, 18, 25] }[/color]
          >
          > sizes = {
          > small : [16, 17, 19],
          > medium : [16, 18, 23],
          > large : [16, 18, 25]
          > };
          >
          > sizes['large'][1] // 18
          > sizes.medium[0] // 16
          >
          > var col = 'small';
          > sizes[col][2] // 19
          >
          >[snip]
          >
          >Hope that helps,
          >Mike[/color]

          Thanks Mike and Lasse I'll remember this but I don't need it now as
          I'm changing all this client-side data manipulation to server-side
          code as previously recommended [due to the difficulty of setting up
          meaningful data structures which emulate the complexity and ease of
          use of a RDBS in client-side code (30K of data!)]. So much as I enjoy
          using it, I won't be able to use javascript and must change to
          something like c# or vb.net.

          Comment

          • mark4asp

            #6
            Re: Can I create a 2-D associative array - if so how?

            On Sat, 25 Sep 2004 13:54:00 +0200, Lasse Reichstein Nielsen
            <lrn@hotpop.com > wrote:
            [color=blue]
            >mark4asp <mark4asp#kills pam#@ntlworld.c om> writes:
            >[color=green]
            >> Suppose I wanted to create an array that was associative in 2
            >> dimensions. The rows are associated with numbers. The columns with
            >> words.[/color]
            >...[color=green]
            >> sizes = {1:[16, 17, 19], 2:[16, 18, 23], 3:[16, 18, 25] }[/color]
            >...[color=green]
            >> function sizes(Small, Medium, Large) {[/color]
            >
            >I would capitalize "sizes" to show that it is used as a constructor
            >function.
            >[color=green]
            >> this.Small = Small;
            >> this.Medium = Medium
            >> this.Large = Large;
            >> }
            >>[/color]
            >...[color=green]
            >> The problem with that is that the rows are now indexed via a
            >> zero-based index instead of the code I wanted to use (as in the first
            >> definition of sizes above) - accessed via an index starting at 1.[/color]
            >
            >Try
            > var sizes = {1: new Sizes(16, 19, 17),
            > 2: new Sizes(16, 23, 18),
            > 3: new Sizes(16, 25, 18)}
            >[color=green]
            >> Is there a way I could create an associative array to refer to data
            >> item 25 in the array as:
            >>
            >> sizes[3][Large][/color][/color]

            Thanks, this was exactly what I wanted. (My row references don't start
            at zero and there are some gaps in them later. So I really need to
            look up the data according to some associative method). [Or at least I
            did - see my other post]

            But I'm sure this info you've given me will be useful in future, for
            other things.

            PS: There are so few good javascript references available. Most take
            the attitude that javascript will only ever be used for client side
            scripting of the DOM. My 2 big javascript books (Wrox & Goodman) don't
            even mention this kind of thing. The Netscape & MS online books don't
            help much, nor did the old MSDN I had installed.
            [color=blue]
            >'ere you go.
            >
            >When you don't use the "arrayness" of the first coordinate (which basically
            >means that you don't use the "length" property or any method on it that
            >comes from Array.prototype ), then you can just as well use a plain object.
            >
            >/L[/color]

            Comment

            Working...