md array gets 'Cannot assign to '[object]' error

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

    md array gets 'Cannot assign to '[object]' error

    Could someone please tell me the correct way to decalre this
    multidimensiona l array?

    TIA!

    jg

    var n = [
    ["Downtown"] = [ ["airport"] = 25, ["downtown"] = 0 ],
    ["Harahan"] = [ ["airport"] = 10, ["downtown"] =
    20 ],
    ["Kenner"] = [ ["airport"] = 5, ["downtown"] =
    20 ],
    ["Lakefront"] = [ ["airport"] = 20, ["downtown"] =
    15 ],
    ["Marrero"] = [ ["airport"] = 30, ["downtown"] =
    15 ],
    ["Metairie"] = [ ["airport"] = 10, ["downtown"] =
    15 ],
    ["Mid City"] = [ ["airport"] = 20, ["downtown"] =
    5 ],
    ["New Orleans East"] = [ ["airport"] = 40, ["downtown"] = 15 ],
    ["Northshore "] = [ ["airport"] = 60, ["downtown"] = 60 ]
    ];

    for (var i = 0; i < n.length; i++) {
    alert(n[i]["airport"]);
    }



  • Michael Winter

    #2
    Re: md array gets 'Cannot assign to '[object]' error

    On Wed, 22 Sep 2004 18:11:36 -0500, jerrygarciuh
    <designs@no.spa m.nolaflash.com > wrote:
    [color=blue]
    > Could someone please tell me the correct way to decalre this
    > multidimensiona l array?[/color]

    That would be difficult as it's not entirely certain what you're trying to
    achieve.

    To declare an array, indexed only by ordinal number, use the square
    bracket literal notation:

    var arr = [1, 3, 'a string', ['a', 'sub', 'array']];
    arr[0] // 1
    arr[1] // 3
    arr[2] // 'a string'
    arr[3] // ['a', 'sub', 'array']
    arr[3][0] // 'a'
    arr[3][1] // 'sub'
    arr[3][2] // 'array'

    To declare a container, indexed by string, use the object literal notation:

    var obj = {aName : 'a value', secondName : 10};
    obj.aName // 'a value'
    obj.secondName // 10

    The code directly above is often confused for associative arrays or hash
    tables; they are not. They are objects with values assigned to properties.
    The effect is pretty much the same, but it's best to be clear regarding
    the language.

    Of course, you can combine these two, placing array within objects, and
    objects with arrays.

    If you're still not certain, you'll have to explain unambiguously what
    you're trying to do.

    Hope that helps,
    Mike

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

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: md array gets 'Cannot assign to '[object]' error

      "jerrygarci uh" <designs@no.spa m.nolaflash.com > writes:
      [color=blue]
      > Could someone please tell me the correct way to decalre this
      > multidimensiona l array?[/color]

      My *guess* as to what you are trying to achieve is not an array.
      It is: a map from strings to (a map from strings to numbers).

      You can use Javascript objects for that, and object literals to
      declare them:
      ---
      var n = {Downtown : {airport : 25, downtown : 0 },
      Harahan : {airport : 10, downtown : 20},
      Kenner : {airport : 5, downtown : 20},
      Lakefront : {airport : 20, downtown : 15},
      Marrero : {airport : 30, downtown : 15},
      Metairie : {airport : 10, downtown : 15},
      "Mid City" : {airport : 20, downtown : 5},
      "New Orleans East" : {airport : 40, downtown : 15},
      Northshore : {airport : 60, downtown : 60}
      };
      ---
      (quotes are optional around names when they contain only latters)

      Since it is not an array, you can't index it by number. Instead,
      you can enumerate the name of the properties directly
      ---
      for (var i in n) {
      alert(i + ": " + n[i]["airport"]);
      }
      ---

      Good luck
      /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

      • jerrygarciuh

        #4
        Re: md array gets 'Cannot assign to '[object]' error

        Thanks! That was exactly what I was trying to do!
        jg


        "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
        news:wtylt07u.f sf@hotpop.com.. .[color=blue]
        > "jerrygarci uh" <designs@no.spa m.nolaflash.com > writes:
        >[color=green]
        > > Could someone please tell me the correct way to decalre this
        > > multidimensiona l array?[/color]
        >
        > My *guess* as to what you are trying to achieve is not an array.
        > It is: a map from strings to (a map from strings to numbers).
        >
        > You can use Javascript objects for that, and object literals to
        > declare them:
        > ---
        > var n = {Downtown : {airport : 25, downtown : 0 },
        > Harahan : {airport : 10, downtown : 20},
        > Kenner : {airport : 5, downtown : 20},
        > Lakefront : {airport : 20, downtown : 15},
        > Marrero : {airport : 30, downtown : 15},
        > Metairie : {airport : 10, downtown : 15},
        > "Mid City" : {airport : 20, downtown : 5},
        > "New Orleans East" : {airport : 40, downtown : 15},
        > Northshore : {airport : 60, downtown : 60}
        > };
        > ---
        > (quotes are optional around names when they contain only latters)
        >
        > Since it is not an array, you can't index it by number. Instead,
        > you can enumerate the name of the properties directly
        > ---
        > for (var i in n) {
        > alert(i + ": " + n[i]["airport"]);
        > }
        > ---
        >
        > Good luck
        > /L
        > --
        > Lasse Reichstein Nielsen - lrn@hotpop.com
        > DHTML Death Colors:[/color]
        <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>[color=blue]
        > 'Faith without judgement merely degrades the spirit divine.'[/color]


        Comment

        • jerrygarciuh

          #5
          Re: md array gets 'Cannot assign to '[object]' error

          Thanks! I was not familiar with that notation.
          I need to take a closer look at objects in JS.
          I use them often in Perl and PHP (as much as you can with PHP).

          I appreciate you taking the time on my behalf!

          jg


          "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
          news:opser43dmv x13kvk@atlantis ...[color=blue]
          > On Wed, 22 Sep 2004 18:11:36 -0500, jerrygarciuh
          > <designs@no.spa m.nolaflash.com > wrote:
          >[color=green]
          > > Could someone please tell me the correct way to decalre this
          > > multidimensiona l array?[/color]
          >
          > That would be difficult as it's not entirely certain what you're trying to
          > achieve.
          >
          > To declare an array, indexed only by ordinal number, use the square
          > bracket literal notation:
          >
          > var arr = [1, 3, 'a string', ['a', 'sub', 'array']];
          > arr[0] // 1
          > arr[1] // 3
          > arr[2] // 'a string'
          > arr[3] // ['a', 'sub', 'array']
          > arr[3][0] // 'a'
          > arr[3][1] // 'sub'
          > arr[3][2] // 'array'
          >
          > To declare a container, indexed by string, use the object literal[/color]
          notation:[color=blue]
          >
          > var obj = {aName : 'a value', secondName : 10};
          > obj.aName // 'a value'
          > obj.secondName // 10
          >
          > The code directly above is often confused for associative arrays or hash
          > tables; they are not. They are objects with values assigned to properties.
          > The effect is pretty much the same, but it's best to be clear regarding
          > the language.
          >
          > Of course, you can combine these two, placing array within objects, and
          > objects with arrays.
          >
          > If you're still not certain, you'll have to explain unambiguously what
          > you're trying to do.
          >
          > Hope that helps,
          > Mike
          >
          > --
          > Michael Winter
          > Replace ".invalid" with ".uk" to reply by e-mail.[/color]


          Comment

          Working...