Link to home
Start Free TrialLog in
Avatar of yes4me
yes4me

asked on

Multiple dimension array

How do you define a multiple dimension array in Javascript?
Avatar of nzjonboy
nzjonboy

myarray = [
   ["name","Bob"],
   ["age","20"]
  ]


hope this helps

nzjonboy
ASKER CERTIFIED SOLUTION
Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

This way you have the advantage of having all the fields named so you can refer to them by name or index.
Avatar of yes4me

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>