3d array in javascript

H

hardik

how i can set 3*3 array in javascript i have tried this but didnt work

<Script>
var a[2][2][2]=new array()
<\Script>


but it didnt work.


Bye & Best Of Luck.
 
H

Hal Rosser

hardik said:
how i can set 3*3 array in javascript i have tried this but didnt work

<Script>
var a[2][2][2]=new array()
<\Script>


but it didnt work.

var a = new Array();
a[0]= new Array();
a[0][5] = new Array;
a[0][5][72]="<h3>Hello matey</h3>";
document.writeln(a[0][5][72]);
 
H

Hal Rosser

Hal Rosser said:
hardik said:
how i can set 3*3 array in javascript i have tried this but didnt work

<Script>
var a[2][2][2]=new array()
<\Script>


but it didnt work.

var a = new Array();
a[0]= new Array();
a[0][5] = new Array;
a[0][5][72]="<h3>Hello matey</h3>";
document.writeln(a[0][5][72]);

You could loop through each array and declare new arrays for each element,
also
This is kinda clumsy - so I'm sure others will post a better solution.-
where I will take notes.
But the point is: its really an array of arrays [of arrays].... rather than
a single multidimensional array.
 
R

RobG

hardik said on 18/04/2006 2:34 PM AEST:
how i can set 3*3 array in javascript i have tried this but didnt work

<Script>
var a[2][2][2]=new array()
<\Script>

If you already had an array called 'a' with an array at index 2 and
another array at that array's index 2, then you could create an array at
index 2 of that last array.

But you haven't, so you have a script error. Also, the built-in array
object has a capital 'A' (to signify that you can use it as a
constructor perhaps).

To save on typing and potential typos, use an initialiser:

var a = [];
a[2] = [];
a[2][2] = [];
a[2][2][2] = [];


A one dimension array 1x3:

var a = ['A', 'B', 'C'];


A two dimension array 2x3:

var a = [
['A', 'B', 'C'],
['D', 'E', 'F']
];


A three dimension array 2x3x3:

var a = [
[
['a','b','c'],
['d','d','f'],
['g','h','i']
],
[
['j','k','l'],
['m','n','o'],
['p','q','r']
]
]

alert( a[0][1][2] ); // shows f


Keep going and it gets much harder to read...
 
R

Randy Webb

RobG said the following on 4/18/2006 1:57 AM:

A two dimension array 2x3:
var a = [
['A', 'B', 'C'],
['D', 'E', 'F']
];

I think that Rob knows, without reading further, what this post says but
a is not a "two dimension array" as it is a simple array that has arrays
as members. Javascript arrays are linear in fashion and as such you
can't have multi-dimensional arrays.

Sorry Rob, but I had to post for posterity sake :)
 
R

RobG

Randy said:
RobG said the following on 4/18/2006 1:57 AM:

A two dimension array 2x3:
var a = [
['A', 'B', 'C'],
['D', 'E', 'F']
];


I think that Rob knows, without reading further, what this post says but
a is not a "two dimension array" as it is a simple array that has arrays
as members. Javascript arrays are linear in fashion and as such you
can't have multi-dimensional arrays.

Sorry Rob, but I had to post for posterity sake :)

That's fine. Others may say that a is a 2D array (matrix) that is
constructed using 2 one-dimensional JavaScript Array objects. :)
 
R

Randy Webb

RobG said the following on 4/18/2006 9:31 AM:
Randy said:
RobG said the following on 4/18/2006 1:57 AM:

A two dimension array 2x3:
var a = [
['A', 'B', 'C'],
['D', 'E', 'F']
];


I think that Rob knows, without reading further, what this post says
but a is not a "two dimension array" as it is a simple array that has
arrays as members. Javascript arrays are linear in fashion and as such
you can't have multi-dimensional arrays.

Sorry Rob, but I had to post for posterity sake :)

That's fine. Others may say that a is a 2D array (matrix) that is
constructed using 2 one-dimensional JavaScript Array objects. :)

Tis true, but VK might say that "RobG says JS has a multi-dimensional
array" <shudder> <g>
 
V

VK

Randy said:
Tis true, but VK might say that "RobG says JS has a multi-dimensional
array" <shudder> <g>

I believe in the JavaScript Array being Dynamic, Sparse, Jagged and
now, and ever and forever!

:-D

P.S. Please, it is just a joke, not a call for discussion.
 
R

Randy Webb

VK said the following on 4/18/2006 1:37 PM:
I believe in the JavaScript Array being Dynamic, Sparse, Jagged and
now, and ever and forever!

:-D

P.S. Please, it is just a joke, not a call for discussion.

Surprisingly enough, I took it that way :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top