Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Initialize multi-dimensional array

28 views
Skip to first unread message

Archos

unread,
Jan 24, 2012, 6:09:00 PM1/24/12
to
I want to initialize a multidimensional array to a determined value,
i.e. an array of dimensions [2][4] initialized to 1.

I can do:
var a2 = new Array(2); for (var i0=0; i0<2; i0++){ a2[i0]=new
Array(4); }
for (var i0=0; i0<2; i0++){ for (var i1=0; i1<4; i1++){ a2[i0]
[i1]=1; }}

or shorter:
var a2 = new Array(2); for (var i0=0; i0<2; i0++){ a2[i0]=new
Array(4); for (var i1=0; i1<4; i1++){ a2[i0][i1]=1; }}

But is there any another way to do it shorter? Without use `var a2 =
[[1,1,1,1], [1,1,1,1]]`

Thanks in advance!

Denis McMahon

unread,
Jan 25, 2012, 4:56:21 AM1/25/12
to
On Tue, 24 Jan 2012 15:09:00 -0800, Archos wrote:

> I want to initialize a multidimensional array to a determined value,
> i.e. an array of dimensions [2][4] initialized to 1.

var arr = new Array ( new Array(1,2,3,4), new Array(2,4,6,8), new Array
(3,6,9,12) );

Rgds

Denis McMahon

Andreas Bergmaier

unread,
Jan 25, 2012, 5:48:10 AM1/25/12
to
Archos schrieb:
> I want to initialize a multidimensional array to a determined value
>
> I can do:
> var a2 = new Array(2); for (var i0=0; i0<2; i0++){ a2[i0]=new
> Array(4); }
> for (var i0=0; i0<2; i0++){ for (var i1=0; i1<4; i1++){ a2[i0]
> [i1]=1; }}
>
> or shorter:
> var a2 = new Array(2); for (var i0=0; i0<2; i0++){ a2[i0]=new
> Array(4); for (var i1=0; i1<4; i1++){ a2[i0][i1]=1; }}
>
> But is there any another way to do it shorter? Without use `var a2 =
> [[1,1,1,1], [1,1,1,1]]`

What do you mean with "shorter"? Less code? Faster execution? If you
don't want to use array literals, there will always be two loops:

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

There might be tricky expressions with less code, but you don't really
want them.

Bergi

Archos

unread,
Jan 25, 2012, 7:37:30 AM1/25/12
to
I mean less code, and using simple '[]' is saved some space, but is it
implemented in the main browsers or I could have any problem by its
use?

Which has a faster execution (array literal or '[]')?

Richard Cornford

unread,
Jan 25, 2012, 9:35:34 AM1/25/12
to
On Jan 25, 12:37 pm, Archos wrote:
> On Jan 25, 10:48 am, Andreas Bergmaier wrote:
<snip>
>> There might be tricky expressions with less code, but
>> you don't really want them.
>
> I mean less code,

Why, what is the problem with having more code?

> and using simple '[]' is saved some space, but is it
> implemented in the main browsers or I could have any problem
> by its use?

Array literals where formalised in ECMA 262 3rd Ed at the end
of 1999, so has been around for long enough to be universally
implemented in web browser's javascript engines.

> Which has a faster execution (array literal or '[]')?

[] is an array literal; a literal declaration for an empty array.

Richard.

Michael Haufe (TNO)

unread,
Jan 25, 2012, 12:38:30 PM1/25/12
to
Use an Array Comprehension.

Thomas 'PointedEars' Lahn

unread,
Jan 25, 2012, 1:39:02 PM1/25/12
to
Andreas Bergmaier wrote:

> Archos schrieb:
>> I want to initialize a multidimensional array to a determined value
>>
>> I can do:
>> var a2 = new Array(2); for (var i0=0; i0<2; i0++){ a2[i0]=new
>> Array(4); }
>> for (var i0=0; i0<2; i0++){ for (var i1=0; i1<4; i1++){ a2[i0]
>> [i1]=1; }}
>>
>> or shorter:
>> var a2 = new Array(2); for (var i0=0; i0<2; i0++){ a2[i0]=new
>> Array(4); for (var i1=0; i1<4; i1++){ a2[i0][i1]=1; }}
>>
>> But is there any another way to do it shorter? Without use `var a2 =
>> [[1,1,1,1], [1,1,1,1]]`
>
> What do you mean with "shorter"? Less code? Faster execution? If you
> don't want to use array literals, there will always be two loops:

No.

> var a=[];for(var i=0;i<2;i++){a[i]=[];for(var j=0;j<4;j++)a[i][j]=1;}
^^ ^^
JFYI: You are using two Array literals here, of course.

> There might be tricky expressions with less code, but you don't really
> want them.

A matter of opinion. Surely

var a = [];
a.length = 5;
a = a.join("1").split("").map(function (el) { return +el; });
a = [a, a.slice()];

is a more elegant solution to this problem.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16

Dr J R Stockton

unread,
Jan 25, 2012, 2:33:41 PM1/25/12
to
In comp.lang.javascript message <81f35d2e-80d0-45aa-965a-c0093cbf4fbf@b2
3g2000yqn.googlegroups.com>, Tue, 24 Jan 2012 15:09:00, Archos
<raul...@sent.com> posted:
Slightly. Use [] instead of new Array(n). A while loop counting down
is probably shorter & faster than a for loop counting up, and you can
combine the decrement and the "was that zero?" test.

var X, j, k, A = []
j = 2 ; do { A[--j] = X = []
k = 4 ; do { X[--k] = 1 }
while (k) }
while (j) ;

if your '4' is both small and constant, and you don't mind setting to
"1" rather than 1, this is shorter

var X, j, A = []
j = 2 ; do { A[--j] = X = "1111".split("") } while (j) ;

and if it is variable but under about 54, and ditto,

var X, j, A = []
j = 2, k = 99
do { A[--j] = X = (Math.pow(2, k)-1).toString(2).split("") } while (j) ;

Inadequately tested.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Website <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

Ross McKay

unread,
Jan 25, 2012, 7:56:11 PM1/25/12
to
On Wed, 25 Jan 2012 19:33:41 +0000, Dr J R Stockton wrote:

>[...] A while loop counting down
>is probably shorter & faster than a for loop counting up, [...]

No.

http://jsperf.com/caching-array-length/4
--
Ross McKay, Toronto, NSW Australia
"You can't control what you can't measure" - Tom DeMarco

Dr J R Stockton

unread,
Jan 27, 2012, 3:09:53 PM1/27/12
to
In comp.lang.javascript message <3g81i7l6qma04ij6jsef0hjprjochsvq4q@4ax.
com>, Thu, 26 Jan 2012 11:56:11, Ross McKay <au.org.zeta.at.rosko@invali
d.invalid> posted:

>On Wed, 25 Jan 2012 19:33:41 +0000, Dr J R Stockton wrote:
>
>>[...] A while loop counting down
>>is probably shorter & faster than a for loop counting up, [...]
>
>No.
>
>http://jsperf.com/caching-array-length/4


I prefer to rely on my own tests, done with a large enough loop for time
to begin to matter.

var A, J, K_ = 777 // increase v put code in those
D0_ = new Date()
Q_ = K_ ;
D1_ = new Date()
Q_ = K_ ; while (Q_--) { }
D2_ = new Date()
Q_ = K_ ; while (Q_--) { A = [] ; J = K_ ; while (--J) A[J] = J }
D3_ = new Date()
Q_ = K_ ; while (Q_--) { A = [] ; for (J = 0 ; J < K_ ; J++) A[J] = J }
D4_ = new Date()
Q_ = [D1_-D0_, D2_-D1_, D3_-D2_, D4_-D3_] // times // Demo 6

The while loop is clearly shorter. And I find it faster in three
browsers out of five.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

Thomas 'PointedEars' Lahn

unread,
Jan 27, 2012, 7:42:42 PM1/27/12
to
Thomas 'PointedEars' Lahn wrote:

> Andreas Bergmaier wrote:
>> var a=[];for(var i=0;i<2;i++){a[i]=[];for(var j=0;j<4;j++)a[i][j]=1;}
> ^^ ^^
> JFYI: You are using two Array literals here, of course.
>
>> There might be tricky expressions with less code, but you don't really
>> want them.
>
> A matter of opinion. Surely
>
> var a = [];
> a.length = 5;
> a = a.join("1").split("").map(function (el) { return +el; });
> a = [a, a.slice()];
>
> is a more elegant solution to this problem.

It should be noted that this approach can also be used with multi-digit
numbers and any other value:

var a = [];
a.length = 5;
a = a.join(",12").split(",").map(function (el) { return +el; });
a.shift();
a = [a, a.slice()];

However more elegant, the map() method is not backwards compatible (so it
requires comparably expensive emulation) and it is expensive itself because
the function needs to be called multiple times. Also, the need for
additional shift() (or pop(), if you use "12," instead) with multi-character
values reduces its elegance.

So a combination of the classical looping approach and the more elegant
slicing approach (which performs the element assignment natively, therefore
should be cheaper than an explicit loop) appears to be the best solution:

function getFilledMatrix(rows, columns, fill)
{
var a = [];

var tmp = [];
tmp.length = columns;

for (var j = 0; j < columns; ++j)
{
tmp[j] = fill || 0;
}

for (var i = 0; i < rows; ++i)
{
if (i == 0)
{
a.push(tmp);
}
else
{
a.push(tmp.slice());
}
}

return a;
}


PointedEars
--
When all you know is jQuery, every problem looks $(olvable).
0 new messages