Velocity Reviews > Create a multi-dimensional array

# Create a multi-dimensional array

Archos
Guest
Posts: n/a

 11-23-2011
To create a two dimensional array I use:

var a4 = new Array(3); for (i=0; i<3; i++) a4[i]=new Array(5);

In pseudoce: a4 = [3][5]

Then, to create a 3 dim. array, I tried this one:

var a5 = new Array(2); for (i=0; i<2; i++) a5[i]=new Array(2); for
(i=0; i<2; i++) a5[i]=new Array(2);

a5 = [2][2][2]

but the firebug console shows that it has not been defined the las
dimension

>>> console.log(a5[1][0])

undefined

Jukka K. Korpela
Guest
Posts: n/a

 11-23-2011
2011-11-23 13:04, Archos wrote:

> To create a two dimensional array I use:
>
> var a4 = new Array(3); for (i=0; i<3; i++) a4[i]=new Array(5);
>
> In pseudoce: a4 = [3][5]

The value of a4 is an array, where the elements are arrays. Calling it a
two-dimensional array probably confuses rather than helps. The notation
[3][5] is not meaningful as such.

> Then, to create a 3 dim. array, I tried this one:
>
> var a5 = new Array(2); for (i=0; i<2; i++) a5[i]=new Array(2); for
> (i=0; i<2; i++) a5[i]=new Array(2);

I don't see what you regard as three-dimensional here, and I don't
understand why you have the same 'for' loop twice.

>>>> console.log(a5[1][0])

> undefined

Naturally, because no value has been assigned to it. You have created an
array with two elements, each of which is an array of two elements, each
of which is initially undefined. Try setting e.g.
a5[1] = ['Hello world', 42]
or
a5[1][0] = 'foobar'
and you'll see that a5 works fine as a simulation of a two-dimensional
array.

--
Yucca, http://www.cs.tut.fi/~jkorpela/

Erwin Moller
Guest
Posts: n/a

 11-23-2011
On 11/23/2011 12:04 PM, Archos wrote:
> To create a two dimensional array I use:
>
> var a4 = new Array(3); for (i=0; i<3; i++) a4[i]=new Array(5);
>
> In pseudoce: a4 = [3][5]
>
> Then, to create a 3 dim. array, I tried this one:
>
> var a5 = new Array(2); for (i=0; i<2; i++) a5[i]=new Array(2); for
> (i=0; i<2; i++) a5[i]=new Array(2);
>

Hi Archos,

All on 1 line, and no {} to make your intentions clear.

Did you mean to do this?
var a5 = new Array(2);
for (i=0; i<2; i++){
a5[i]=new Array(2);
for (i=0; i<2; i++){
a5[i]=new Array(2);
}
}

That would be wrong of course.
2 times i, and the second loop also does something strange with the
indexes of a5.

What you want, however, is probably something like this:

var a5 = new Array(2);
for (var i=0; i<2; i++){
a5[i]=new Array(2);
for (var j=0; j<2; j++){
a5[i][j]=new Array(2);
}
}

That uses i and j to make things clearer and uses "var".
Now you can say:
a5[1][1][0] = "whatever";

Regards,
Erwin Moller

> a5 = [2][2][2]
>
> but the firebug console shows that it has not been defined the las
> dimension
>
>>>> console.log(a5[1][0])

> undefined

--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens

Archos
Guest
Posts: n/a

 11-23-2011
On Nov 23, 11:46 am, Erwin Moller
> What you want, however, is probably something like this:
>
> var a5 = new Array(2);
> for (var i=0; i<2; i++){
> a5[i]=new Array(2);
> for (var j=0; j<2; j++){
> a5[i][j]=new Array(2);
> }
>
> }
>
> That uses i and j to make things clearer and uses "var".

Could be used "let" instead of "var"? since it's for local scope.

Thanks!

John G Harris
Guest
Posts: n/a

 11-23-2011
On Wed, 23 Nov 2011 at 13:41:28, in comp.lang.javascript, Jukka K.
Korpela wrote:

<snip>
>and you'll see that a5 works fine as a simulation of a two-dimensional
>array.

It's more than a 'simulation'. If it's a structure that correctly
implements the job of a two-dimensional array then it *is* a two
dimensional array.

Just because programs are not built into the language doesn't mean that
programs are mere 'simulations'.

John
--
John Harris

Denis McMahon
Guest
Posts: n/a

 11-23-2011
On Wed, 23 Nov 2011 03:04:49 -0800, Archos wrote:

> To create a two dimensional array I use:
>
> var a4 = new Array(3); for (i=0; i<3; i++) a4[i]=new Array(5);
>
> In pseudoce: a4 = [3][5]
>
> Then, to create a 3 dim. array, I tried this one:
>
> var a5 = new Array(2); for (i=0; i<2; i++) a5[i]=new Array(2); for
> (i=0; i<2; i++) a5[i]=new Array(2);
>
> a5 = [2][2][2]

Yes, because all you've done is define a5[i] twice. I think that what you
want to do is define a5[i][?] as an array?

Try (untested):

var a5 = new Array(2);
for (var i=0; i<2; i++)
{
a5[i]=new Array(2);
for (var j=0; j<2; j++) a5[i][j]=new Array(2);
}

Rgds

Denis McMahon

Thomas 'PointedEars' Lahn
Guest
Posts: n/a

 11-23-2011
Denis McMahon wrote:

> var a5 = new Array(2);
> for (var i=0; i<2; i++)
> {
> a5[i]=new Array(2);
> for (var j=0; j<2; j++) a5[i][j]=new Array(2);
> }

Using arguments for the Array constructor is pointless here and error-prone.
For that matter, using the Array constructor is unnecessary and potentially
error-prone. Use the Array initializer instead; it is most certainly
ubiquitous by now [1]:

var a5 = [];
for (var i = 0; i < 2; ++i)
{
a5[i] = [];
for (var j = 0; j < 2; ++j)
{
a5[i][j] = [];
}
}

Note that you can save memory if you do not create all potentially needed
Array instances, but you have to pay for that with a bit of runtime (tests
before access whether there already is an Array instance reference
assigned).

PointedEars
___________
[1] <http://PointedEars.de/es-matrix/#!>
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q\$253\$1\$(E-Mail Removed)> (2004)

Dr J R Stockton
Guest
Posts: n/a

 11-24-2011
In comp.lang.javascript message <jaim56\$151\$(E-Mail Removed)>, Wed, 23
Nov 2011 13:41:28, Jukka K. Korpela <(E-Mail Removed)> posted:

>2011-11-23 13:04, Archos wrote:
>
>> To create a two dimensional array I use:
>>
>> var a4 = new Array(3); for (i=0; i<3; i++) a4[i]=new Array(5);
>>
>> In pseudoce: a4 = [3][5]

>
>The value of a4 is an array, where the elements are arrays. Calling it
>a two-dimensional array probably confuses rather than helps.

It will help if the reader is accustomed to languages which support
multi-dimensional arrays, and wants in JavaScript the effect of a two-
dimensional array. Those languages include Algol (IIRC), Pascal/Delphi,
and English.

> The notation [3][5] is not meaningful as such.

The author meant to say that it is pseudocode. It's certainly
meaningful in some dialects of pseudocode.

>Naturally, because no value has been assigned to it. You have created
>an array with two elements, each of which is an array of two elements,
>each of which is initially undefined. Try setting e.g.
>a5[1] = ['Hello world', 42]
>or
>a5[1][0] = 'foobar'
>and you'll see that a5 works fine as a simulation of a two-dimensional
>array.

Provided that one recalls that a5 must be an array (maybe empty) before
the first of those assignments, and that a5[1] must be an array (maybe
empty) before the second of those assignments.

--
(c) John Stockton, nr London UK ?@merlyn.demon.co.uk IE8 FF8 Op11 Sf5 Cr15
news:comp.lang.javascript FAQ <http://www.jibbering.com/faq/index.html>.
<http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.

Jukka K. Korpela
Guest
Posts: n/a

 11-24-2011
2011-11-24 20:42, Dr J R Stockton wrote:

>> The value of a4 is an array, where the elements are arrays. Calling it
>> a two-dimensional array probably confuses rather than helps.

>
> It will help if the reader is accustomed to languages which support
> multi-dimensional arrays,

Sounds like confusion. Do you really wish to promote such ideas?

>> The notation [3][5] is not meaningful as such.

>
> The author meant to say that it is pseudocode. It's certainly
> meaningful in some dialects of pseudocode.

Nonsense does not become any more meaningful by someone's calling it
"pseudocode".

> Provided that one recalls that a5 must be an array (maybe empty) before
> the first of those assignments, and that a5[1] must be an array (maybe
> empty) before the second of those assignments.

Are you trying to make a point, or just noise?

--
Yucca, http://www.cs.tut.fi/~jkorpela/

Gene Wirchenko
Guest
Posts: n/a

 11-25-2011
On Fri, 25 Nov 2011 01:53:58 +0200, "Jukka K. Korpela"
<(E-Mail Removed)> wrote:

>2011-11-24 20:42, Dr J R Stockton wrote:
>
>>> The value of a4 is an array, where the elements are arrays. Calling it
>>> a two-dimensional array probably confuses rather than helps.

>>
>> It will help if the reader is accustomed to languages which support
>> multi-dimensional arrays,

>
>Sounds like confusion. Do you really wish to promote such ideas?
>
>>> The notation [3][5] is not meaningful as such.

>>
>> The author meant to say that it is pseudocode. It's certainly
>> meaningful in some dialects of pseudocode.

>
>Nonsense does not become any more meaningful by someone's calling it
>"pseudocode".

At least two of us understood it just fine. Check your language
calibration?

[snip]

Sincerely,

Gene Wirchenko