Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #10661
| Message-ID | <2236669.bzG4ezAXRf@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2012-01-28 01:42 +0100 |
| Subject | Re: Initialize multi-dimensional array |
| Newsgroups | comp.lang.javascript |
| References | <81f35d2e-80d0-45aa-965a-c0093cbf4fbf@b23g2000yqn.googlegroups.com> <jfoml6$ve6$1@news.albasani.net> <1934720.aqSPkdTlGX@PointedEars.de> |
| Followup-To | comp.lang.javascript |
Followups directed to: comp.lang.javascript
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).
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Initialize multi-dimensional array Archos <raul.san@sent.com> - 2012-01-24 15:09 -0800
Re: Initialize multi-dimensional array Denis McMahon <denismfmcmahon@gmail.com> - 2012-01-25 09:56 +0000
Re: Initialize multi-dimensional array Andreas Bergmaier <andber93@web.de> - 2012-01-25 11:48 +0100
Re: Initialize multi-dimensional array Archos <raul.san@sent.com> - 2012-01-25 04:37 -0800
Re: Initialize multi-dimensional array Richard Cornford <Richard@litotes.demon.co.uk> - 2012-01-25 06:35 -0800
Re: Initialize multi-dimensional array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-25 19:39 +0100
Re: Initialize multi-dimensional array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-28 01:42 +0100
Re: Initialize multi-dimensional array "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2012-01-25 09:38 -0800
Re: Initialize multi-dimensional array Dr J R Stockton <reply1204@merlyn.demon.co.uk> - 2012-01-25 19:33 +0000
Re: Initialize multi-dimensional array Ross McKay <au.org.zeta.at.rosko@invalid.invalid> - 2012-01-26 11:56 +1100
Re: Initialize multi-dimensional array Dr J R Stockton <reply1204@merlyn.demon.co.uk> - 2012-01-27 20:09 +0000
csiph-web