how define array of array?

G

Geoff Cox

Hello,

This may be the answer to a problem I have - or it may not be
possible?!

Can I have initial values of variable situation and count defined as

var situation = 0;
var count = 0;

when during the app situation will vary from 0 to 7 and count from 0
to 8,

and have

value[situation][count] ?

If yes, how do I define the array if arrays?

Thanks

Geoff
 
B

Baconbutty

In Javascript Arrays are "flat", so to make multiple dimensions, you
create an Array as an item of another Array, thus:-

// AN ARRAY CAN BE AN ELEMENT OF ANOTHER ARRAY
var value=new Array();
value[0]=new Array();
value[0][0]="value00";
value[0][1]="value01";
value[1]=new Array();
value[1][0]="value10";
value[1][1]="value11";
alert (value[0][1]);


// SHORTHAND JSON NOTATION
var value=[]; // SAME AS var value=new Array

// EQUIVALENT TO THE ABOVE
var value=[ ["value00","value01"] , ["value10","value11"] ];
alert (value[0][1]);
 
D

Dag Sunde

Geoff Cox said:
Hello,

This may be the answer to a problem I have - or it may not be
possible?!

Can I have initial values of variable situation and count defined as

var situation = 0;
var count = 0;

when during the app situation will vary from 0 to 7 and count from 0
to 8,

and have

value[situation][count] ?

If yes, how do I define the array if arrays?

Thanks

var value = new Array(8);
for (var n = 0; n < value.length; n++ ) {
value[n] = new Array(9);
}
 
G

Geoff Cox

In Javascript Arrays are "flat", so to make multiple dimensions, you
create an Array as an item of another Array, thus:-

// AN ARRAY CAN BE AN ELEMENT OF ANOTHER ARRAY
var value=new Array();
value[0]=new Array();
value[0][0]="value00";
value[0][1]="value01";
value[1]=new Array();
value[1][0]="value10";
value[1][1]="value11";
alert (value[0][1]);


// SHORTHAND JSON NOTATION
var value=[]; // SAME AS var value=new Array

// EQUIVALENT TO THE ABOVE
var value=[ ["value00","value01"] , ["value10","value11"] ];
alert (value[0][1]);

Thanks for this - perhaps you could see my last posting to Dag and
comment?

Cheers

Geoff
 
B

Baconbutty

Arrays do not need to have the length specified. The "length" will be
one more than the last integer index (i.e. counting index position 0).

Thus:-

var a=new Array();
a[10]="Item11";
alert(a.length); // 11 even though 0 - 9 are undefined

Hence what is important is populating those arrays, not declaring them,
as it is only when you populate them that items are effectively
created.

If you search this group for words such as Array, Hash, [[PUT]] and
writings from those such as Richard Cornford, Michael Winter etc there
is a lot of technical information (and argument) about the ECMA Array
object which you may find helpful.
 
G

Geoff Cox

Arrays do not need to have the length specified. The "length" will be
one more than the last integer index (i.e. counting index position 0).

Thus:-

var a=new Array();
a[10]="Item11";
alert(a.length); // 11 even though 0 - 9 are undefined

Hence what is important is populating those arrays, not declaring them,
as it is only when you populate them that items are effectively
created.

If you search this group for words such as Array, Hash, [[PUT]] and
writings from those such as Richard Cornford, Michael Winter etc there
is a lot of technical information (and argument) about the ECMA Array
object which you may find helpful.

Thanks for above and text from others ..

Cheers

Geoff
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top