yes4me
asked on
Multiple dimension array
How do you define a multiple dimension array in Javascript?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Or like this...
We create an array "banner" of banner ads with info about the URL, IMG and ALT value...
function bannerAd(url, alt, img)
{
this.url = url;
this.alt = alt;
this.img = img;
}
banner = new Array(
new bannerAd("http://www.amazon.com/free-books.html",
"Free Books Exchange",
"http://www.mydomain.com/ads/amazon1.gif"),
new bannerAd("http://www.amazon.com/cheap-books.html",
"Cheap Books Exchange",
"http://www.mydomain.com/ads/amazon2.gif"),
new bannerAd("http://www.amazon.com/collectors-books.html",
"Collectors Books Exchange",
"http://www.mydomain.com/ads/amazon3.gif")
);
Now you can retrieve the info like this...
banner[0].url
banner[0].alt
banner[0].img
We create an array "banner" of banner ads with info about the URL, IMG and ALT value...
function bannerAd(url, alt, img)
{
this.url = url;
this.alt = alt;
this.img = img;
}
banner = new Array(
new bannerAd("http://www.amazon.com/free-books.html",
"Free Books Exchange",
"http://www.mydomain.com/ads/amazon1.gif"),
new bannerAd("http://www.amazon.com/cheap-books.html",
"Cheap Books Exchange",
"http://www.mydomain.com/ads/amazon2.gif"),
new bannerAd("http://www.amazon.com/collectors-books.html",
"Collectors Books Exchange",
"http://www.mydomain.com/ads/amazon3.gif")
);
Now you can retrieve the info like this...
banner[0].url
banner[0].alt
banner[0].img
This way you have the advantage of having all the fields named so you can refer to them by name or index.
ASKER
I like defining my array using new Array()... and since you are the first one to use them, you get the points. But seriously, you guys are all right.
What you have all confirmed me one thing is that there is not a very nice way to define a multidimension array. Here is what I end up with:
<SCRIPT>
multiArray = new Array(3)
for (i=0; i<3; i++)
multiArray[i] = new Array(4)
for (i=0; i<multiArray.length; i++)
for (j=0; j<multiArray[0].length; j++)
multiArray[i][j] = i+"/"+j
for (i=0; i<3; i++)
{
for (j=0; j<4; j++)
document.write(i +"/"+ j +": "+ multiArray[i][j] +"<BR>")
document.write("<BR>")
}
</SCRIPT>
What you have all confirmed me one thing is that there is not a very nice way to define a multidimension array. Here is what I end up with:
<SCRIPT>
multiArray = new Array(3)
for (i=0; i<3; i++)
multiArray[i] = new Array(4)
for (i=0; i<multiArray.length; i++)
for (j=0; j<multiArray[0].length; j++)
multiArray[i][j] = i+"/"+j
for (i=0; i<3; i++)
{
for (j=0; j<4; j++)
document.write(i +"/"+ j +": "+ multiArray[i][j] +"<BR>")
document.write("<BR>")
}
</SCRIPT>
["name","Bob"],
["age","20"]
]
hope this helps
nzjonboy