By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,903 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,903 IT Pros & Developers. It's quick & easy.

how define array of array?

P: n/a
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
Sep 14 '05 #1
Share this Question
Share on Google+
20 Replies


P: n/a
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]);

Sep 14 '05 #2

P: n/a
"Geoff Cox" <ge*******@notquitecorrectfreeuk.com> wrote in message
news:ti********************************@4ax.com...
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);
}

--
Dag.
Sep 14 '05 #3

P: n/a
On Wed, 14 Sep 2005 08:34:40 GMT, "Dag Sunde" <me@dagsunde.com> wrote:

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


Dag,

Thanks for above - it has helped me to get the app working but I am
not quite clear how yet?!

The actual code I use is

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

You will see I use Array(8) rather than Array(9).

The array I am using to collect some slider values, is

slider_value[situation_number][count]

where situation_number varies between 0 and 6

and

count varies between 0 and 7.

Does the defination of the array or arrays above fit in with this?

Cheers

Geoff
Sep 14 '05 #4

P: n/a
On 14 Sep 2005 01:29:17 -0700, "Baconbutty" <ju****@baconbutty.com>
wrote:
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

Sep 14 '05 #5

P: n/a
Geoff Cox said the following on 9/14/2005 5:14 AM:
On Wed, 14 Sep 2005 08:34:40 GMT, "Dag Sunde" <me@dagsunde.com> wrote:
var value = new Array(8);
for (var n = 0; n < value.length; n++ ) {
value[n] = new Array(9);
}

Dag,

Thanks for above - it has helped me to get the app working but I am
not quite clear how yet?!

The actual code I use is

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

You will see I use Array(8) rather than Array(9).

The array I am using to collect some slider values, is

slider_value[situation_number][count]

where situation_number varies between 0 and 6

and

count varies between 0 and 7.

Does the defination of the array or arrays above fit in with this?


It's irrelevant actually.

for (var n = 0; n < slider_value.length; n++ ) {
slider_value[n] = new Array();
}

Works just as well. Note the lack of a size inside the Array().

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 14 '05 #6

P: n/a
"Randy Webb" <Hi************@aol.com> wrote in message
news:LY********************@comcast.com...
Geoff Cox said the following on 9/14/2005 5:14 AM:
On Wed, 14 Sep 2005 08:34:40 GMT, "Dag Sunde" <me@dagsunde.com> wrote:
var value = new Array(8);
for (var n = 0; n < value.length; n++ ) {
value[n] = new Array(9);
}

Dag,

Thanks for above - it has helped me to get the app working but I am
not quite clear how yet?!

The actual code I use is

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

You will see I use Array(8) rather than Array(9).

The array I am using to collect some slider values, is

slider_value[situation_number][count]

where situation_number varies between 0 and 6

and

count varies between 0 and 7.

Does the defination of the array or arrays above fit in with this?


It's irrelevant actually.

for (var n = 0; n < slider_value.length; n++ ) {
slider_value[n] = new Array();
}

Works just as well. Note the lack of a size inside the Array().

As Randy say, Javascript doesn't need to be told the size of an array.
As long as the variable is declared as an array, it will create an element
as soon as you refer to it, if it isn't created already.

The reason I use it is twofold; I find it (in most situations) clearer
to read my code when I explicitly define the size (If applicable).
The second reason is that my workday consists of using several other
languages in addition to JavaScript that *do* require the size of arrays...

--
Dag.
Sep 14 '05 #7

P: n/a
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.

Sep 14 '05 #8

P: n/a
On 14 Sep 2005 04:41:50 -0700, "Baconbutty" <ju****@baconbutty.com>
wrote:
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

Sep 14 '05 #9

P: n/a
Dag Sunde wrote:
[...]

As Randy say, Javascript doesn't need to be told the size of an array.
As long as the variable is declared as an array, it will create an element
as soon as you refer to it, if it isn't created already.

The reason I use it is twofold; I find it (in most situations) clearer
to read my code when I explicitly define the size (If applicable).
The second reason is that my workday consists of using several other
languages in addition to JavaScript that *do* require the size of arrays...

To round this out it's worth mentioning that creating an array using:

var someNames = ['fred', 'sue', 'dave'];

is called an 'object intitializer' or 'literal notation', it's
essentially the same as creating the array by calling the constructor
function and then populating it:

var someNames = new Array(3);
someNames[0] = 'fred';
someNames[1] = 'sue';
someNames[2] = 'dave';

<URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Creating_New_Objects:Usi ng_Object_Initializers>
Using a length in the constructor is redundant and could create
problems that don't need to be created (I'm thinking of lazy cut 'n
paste developers who leave undefined elements in an array and don't
properly deal with them).

Initializers seem to save a lot of typing, even if they are only used
to declare an empty array:

var x = [];

versus:

var x = new Array()


--
Rob
Sep 14 '05 #10

P: n/a
Randy Webb wrote:


It's irrelevant actually.

for (var n = 0; n < slider_value.length; n++ ) {
slider_value[n] = new Array();
}

Works just as well. Note the lack of a size inside the Array().


Logically the user agent may interpret new Array(8) as an array of
containing one element, i.e. a[0]==8
Mick
Sep 14 '05 #11

P: n/a
Mick White said the following on 9/14/2005 5:35 PM:
Randy Webb wrote:


It's irrelevant actually.

for (var n = 0; n < slider_value.length; n++ ) {
slider_value[n] = new Array();
}

Works just as well. Note the lack of a size inside the Array().


Logically the user agent may interpret new Array(8) as an array of
containing one element, i.e. a[0]==8


How is that logical?

If there is a number present in the Array(), then it is the length of an
empty array that is created.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 14 '05 #12

P: n/a
Randy Webb wrote:
Mick White said the following on 9/14/2005 5:35 PM:
Randy Webb wrote:


It's irrelevant actually.

for (var n = 0; n < slider_value.length; n++ ) {
slider_value[n] = new Array();
}

Works just as well. Note the lack of a size inside the Array().


Logically the user agent may interpret new Array(8) as an array of
containing one element, i.e. a[0]==8

How is that logical?

If there is a number present in the Array(), then it is the length of an
empty array that is created.

What about: var a=[8] ?
And are you sure that every js version follows this rule?
Mick
Sep 14 '05 #13

P: n/a
Randy Webb wrote:
Mick White said the following on 9/14/2005 5:35 PM:
Randy Webb wrote: [...]


Logically the user agent may interpret new Array(8) as an array of
containing one element, i.e. a[0]==8

How is that logical?

If there is a number present in the Array(), then it is the length of an
empty array that is created.


http://www.devguru.com/Technologies/...ref/array.html
<<If you specify 'language="Javascript1.2"' in the <SCRIPT>
tag and use a single numeric parameter with the Array constructor,
it will be seen as the value of a single element of the array
rather than the number of elements you want that array to
contain.>>
Mick

Sep 14 '05 #14

P: n/a
ASM
Mick White wrote:
Randy Webb wrote:


It's irrelevant actually.

for (var n = 0; n < slider_value.length; n++ ) {
slider_value[n] = new Array();
}

Works just as well. Note the lack of a size inside the Array().


Logically the user agent may interpret new Array(8) as an array of
containing one element, i.e. a[0]==8


What logical in conveniences to code ?

a = new Array(11); ==> new array with 11 elements
a[0] = 11; ==> its 1st element value is '11'

i = new Image(420,300) ==> new image 420px width and 300px height
(it is not the value of image neither its name)

I never have understood interest to give this length while setting a new array
when we know we can change it at any time

--
Stephane Moriaux et son [moins] vieux Mac
Sep 14 '05 #15

P: n/a
ASM
Mick White wrote:
http://www.devguru.com/Technologies/...ref/array.html
<<If you specify 'language="Javascript1.2"' in the <SCRIPT> tag and use
a single numeric parameter with the Array constructor, it will be
seen as the value of a single element of the array rather
than the number of elements you want that array to contain.>>
Mick


Mais ils m'en inventent tous les jours !

ouf! it was not specified !
--
Stephane Moriaux et son [moins] vieux Mac
Sep 14 '05 #16

P: n/a
Mick White said the following on 9/14/2005 6:14 PM:
Randy Webb wrote:
Mick White said the following on 9/14/2005 5:35 PM:
Randy Webb wrote:

It's irrelevant actually.

for (var n = 0; n < slider_value.length; n++ ) {
slider_value[n] = new Array();
}

Works just as well. Note the lack of a size inside the Array().
Logically the user agent may interpret new Array(8) as an array of
containing one element, i.e. a[0]==8


How is that logical?

If there is a number present in the Array(), then it is the length of
an empty array that is created.

What about: var a=[8] ?
And are you sure that every js version follows this rule?


Can you name one that doesn't?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 15 '05 #17

P: n/a
Mick White said the following on 9/14/2005 6:19 PM:
Randy Webb wrote:
Mick White said the following on 9/14/2005 5:35 PM:
Randy Webb wrote:
[...]
Logically the user agent may interpret new Array(8) as an array of
containing one element, i.e. a[0]==8


How is that logical?

If there is a number present in the Array(), then it is the length of
an empty array that is created.

http://www.devguru.com/Technologies/...ref/array.html
<<If you specify 'language="Javascript1.2"' in the <SCRIPT> tag and use
a single numeric parameter with the Array constructor, it will be
seen as the value of a single element of the array rather
than the number of elements you want that array to contain.>>


And that is part of why the language attribute is deprecated. But in
javascript1.2, the = is a comparison as well:

y=2

if (y=3){
alert('true')
}

You will get the alert in modern browsers, in NN4, you get no alert
because it is comparing whereas other browsers are saying "Yes, you can
assign 3 to y so its true).

So, using language="javascript1.2" is hardly a good defense.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 15 '05 #18

P: n/a
Mick White <mw***********@rochester.rr.com> writes:

[new Array(8) not equivalent to [8]...]
And are you sure that every js version follows this rule?


No, but we are not talking about every javascript function, but about
Array, which has a long and glorious history of always treating a single
number argument as a the length, and everything else as elements, of
the created array.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Sep 15 '05 #19

P: n/a
Lasse Reichstein Nielsen wrote:
Mick White <mw***********@rochester.rr.com> writes:

[new Array(8) not equivalent to [8]...]
And are you sure that every js version follows this rule?

No, but we are not talking about every javascript function, but about
Array, which has a long and glorious history of always treating a single
number argument as a the length, and everything else as elements, of
the created array.


I don't like it, though...
Mick
Sep 15 '05 #20

P: n/a
Mick White <mw***********@rochester.rr.com> writes:
Lasse Reichstein Nielsen wrote:
Mick White <mw***********@rochester.rr.com> writes:
[new Array(8) equals [,,,,,,,,]]
I don't like it, though...


Me neither. It's an unnecessary exception, and that always invites
bugs. But you can't fight history, and Array has been like that since
.... I guess Netscape 3.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Sep 15 '05 #21

This discussion thread is closed

Replies have been disabled for this discussion.