Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #10580

Re: Initialize multi-dimensional array

Message-ID <1934720.aqSPkdTlGX@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-01-25 19:39 +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>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


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

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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