2 dimensional arrays(java script)

Thread: 2 dimensional arrays(java script)

1. Jon Guest

2 dimensional arrays(java script)

It is possible to create 2 dimensional arrays in java script?<BR>If so, how do you go about doing it?<BR>Thank you.

2. Senior Member
Join Date
Dec 1969
Posts
1,431

Not exactly but you can replicate the behaviour

In javascript there is no such thing as a multidimensional array, however you can have an array of arrays. So let&#039;s say we want to create an array of size 3,2<BR><BR>we would do it as follows<BR><BR>var myArr= new Array()<BR><BR>for (i=0; i&#060;4; i++) {<BR> myArr[i] = new Array()<BR> for (j=0; i&#060;3; j++) {<BR> myArr[i][j] = i*j //just some dummy data<BR> }<BR>}<BR><BR>this should work but don&#039;t kill me if it doesn&#039;t I may have some typos.<BR> <BR>

3. Senior Member
Join Date
Dec 1969
Posts
701

RE: 2 dimensional arrays(java script)

Of course it&#039;s possible.<BR><BR>This IS an ASP forum though. Try a JavaScript site for JavaScript questions (www.wdvl.com will do as good as any - or infinitejs.com I have heard some people saying).<BR><BR>However, it&#039;s very easy<BR><BR>var x;<BR>x[0][0] = 1;<BR>x[0][1] = 2;<BR>x[1][0] = 3;<BR><BR>etc...<BR><BR>Craig.

4. Senior Member
Join Date
Dec 1969
Posts
1,431

That is actually not a 2 dimensional array

but very similar it&#039;s actually an array of arrays as you can do<BR><BR>x[0][0]=1<BR>x[1][0]=1<BR>x[1][1]=1<BR><BR>ie the second dimension is not fixed in length for each row. Also each item x[0] etc has array properties ie length etc.

5. Senior Member
Join Date
Dec 1969
Posts
701

RE: That is actually not a 2 dimensional array

Yeah, but for simplicity&#039;s sake it can be thought of as a two-dimensional array.<BR><BR>Craig.

6. Senior Member
Join Date
Dec 1969
Posts
1,431

True it does work

in a a very similar fashion and in ways is more powerful, I wonder if it hogs more memory though?? Probably not as can optimize the size for a diagonal matrix say..

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•