Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multidimensional array 2

Status
Not open for further replies.

leahyj

MIS
May 5, 2004
107
0
0
US
I have read on various sites, that javascript doesn't have multidimensional arrays persay, example var myarray = new Array(,) or var myarray = new Array(3,2) or whatever.

Is this true?
 
You can have multidimensional arrays, you just can't define them in a one-liner like you might be able to in other languages.

For example, the following code creates a 2x2 array:
Code:
var arr = new Array(2);
arr[0] = new Array(2);
arr[1] = new Array(2);

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
Yipes,

That's what I thought.


How do I retrieve or set the values.

Which is the column and which is the row?


Thanks
 
This stuff is pretty basic javascript and you can find it all over the internet with a little prodding, did you not find any tutorials on google?

When I searched google for "javascript 2 dimensional arrays" the first link in the list produced very relevant descriptions and examples:


-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
Have you considered JSON?
Code:
var multi = [
   [1,2,3],
   [4,5,6,7],
   [8,9,10]
];

alert(multi[0][2]); // '3'
alert(multi[1][3]); // '7'

________________________________________
Give a man a match, he'll be warm for a minute. But light him on fire, and he'll be warm for the rest of his life.
 
jtreefrog that DEFINITELY deserves a star! Absolutely beautiful! I probably wouldn't have thought of doing it that way, and I use JSON all the time!

leahyj Which do you WANT to be the column and which the row? As long as you are consistent, it really doesn't matter. Those terms simply help conceptualize a to-dimensional table, they're actually meaningless.
The computer doesn't store tables that way in memory anyway. Once you start using 3- and 4-dimensional tables even the term "-dimensional" really become irrelevant.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Thanks :) I've found that JSON makes coding a LOT faster and easier to read over traditional methods.

leahyj: I would consider the first dimension your rows and the second dimension your cells (columns) - especially if you're considering creating an HTML table based on the data in your array.

________________________________________
Give a man a match, he'll be warm for a minute. But light him on fire, and he'll be warm for the rest of his life.
 
Thanks to jtreefrog and tsdragon.

I guess JSON arrays, which was new to me before you mentioned it, is similar or equivalent to literal arrays.

I think I got a handle on the conception of the old style arrays and the literal arrays now.

I was coming from a vb background, and I was having a hard time getting a handle on javascript multidimensional arrays.

I got the rows and columns concept also. Thanks.

 
You can learn more about JSON in this FAQ from the AJAX forum: What is JSON? faq1600-6409

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top