460,044 Members | 1,330 Online
Need help? Post your question and get tips & solutions from a community of 460,044 IT Pros & Developers. It's quick & easy.

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

 P: n/a Could someone please tell me the correct way to decalre this multidimensional 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"]); } Jul 23 '05 #1
4 Replies

 P: n/a On Wed, 22 Sep 2004 18:11:36 -0500, jerrygarciuh wrote: Could someone please tell me the correct way to decalre this multidimensional array? 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. Jul 23 '05 #2

 P: n/a "jerrygarciuh" writes: Could someone please tell me the correct way to decalre this multidimensional array? 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 - lr*@hotpop.com DHTML Death Colors: 'Faith without judgement merely degrades the spirit divine.' Jul 23 '05 #3

 P: n/a Thanks! That was exactly what I was trying to do! jg "Lasse Reichstein Nielsen" wrote in message news:wt**********@hotpop.com... "jerrygarciuh" writes: Could someone please tell me the correct way to decalre this multidimensional array? 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 - lr*@hotpop.com DHTML Death Colors: 'Faith without judgement merely degrades the spirit divine.' Jul 23 '05 #4

 P: n/a 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" wrote in message news:opser43dmvx13kvk@atlantis... On Wed, 22 Sep 2004 18:11:36 -0500, jerrygarciuh wrote: Could someone please tell me the correct way to decalre this multidimensional array? 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. Jul 23 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.