473,398 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

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 1866
On Wed, 22 Sep 2004 18:11:36 -0500, jerrygarciuh
<de*****@no.spam.nolaflash.com> 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
"jerrygarciuh" <de*****@no.spam.nolaflash.com> 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: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #3
Thanks! That was exactly what I was trying to do!
jg
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:wt**********@hotpop.com...
"jerrygarciuh" <de*****@no.spam.nolaflash.com> 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:

<URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'

Jul 23 '05 #4
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.******@blueyonder.co.invalid> wrote in message
news:opser43dmvx13kvk@atlantis...
On Wed, 22 Sep 2004 18:11:36 -0500, jerrygarciuh
<de*****@no.spam.nolaflash.com> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: chessc4c6 | last post by:
The program below creates a char pointer call charPtr...... i then declare an char array string "Good Luck" When i assign charPtr = string, I expect an error. However, It actually runs and...
7
by: James Mcguire | last post by:
Hi, I frequently do non-initialisation type structure assignment via casting: e.g. struct s{int i,j,k;} mys; .... mys=(struct s){3,4,5};
32
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains...
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
1
by: parrot toes | last post by:
I tried to post this question before, but there was an error when posting. I case it did get posted and in order to avoid duplication, I'll just repost a summary. I have written a dotnet client...
4
by: viks | last post by:
Hi guys I need little help here . I want to convert 'System::Object __gc * array ' to 'float array' Lets say I have object Reader with method Send ,it returns a variant that contains a...
2
by: craig | last post by:
I'm fried on this problem...It's vb.net, dealing with a web service that requires input as an array of a particular data structure and I can't get the code to compile or run... Basically, the...
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
11
by: Matthew Wells | last post by:
Hello. I have figured out how to create an instance of an object only knowing the type by string. string sName = "MyClassName"; Type t = Type.GetType(sName); Object objNew =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.