Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #10583
| From | Dr J R Stockton <reply1204@merlyn.demon.co.uk> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Initialize multi-dimensional array |
| Date | 2012-01-25 19:33 +0000 |
| Organization | Home |
| Message-ID | <tWjEMKGVkFIPFwTj@invalid.uk.co.demon.merlyn.invalid> (permalink) |
| References | <81f35d2e-80d0-45aa-965a-c0093cbf4fbf@b23g2000yqn.googlegroups.com> |
In comp.lang.javascript message <81f35d2e-80d0-45aa-965a-c0093cbf4fbf@b2
3g2000yqn.googlegroups.com>, Tue, 24 Jan 2012 15:09:00, Archos
<raul.san@sent.com> posted:
>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]]`
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.
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