Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.alphanet.ch!alphanet.ch!news.imp.ch!news.albasani.net!news.teledata-fn.de!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="ISO-8859-1" Message-ID: <1934720.aqSPkdTlGX@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Wed, 25 Jan 2012 19:39:02 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 7Bit Subject: Re: Initialize multi-dimensional array Newsgroups: comp.lang.javascript References: <81f35d2e-80d0-45aa-965a-c0093cbf4fbf@b23g2000yqn.googlegroups.com> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 46 NNTP-Posting-Date: 25 Jan 2012 19:39:02 CET NNTP-Posting-Host: a9d344af.newsspool3.arcor-online.net X-Trace: DXC=GKe=kAVJ]UUaAeROF2PWMQMcF=Q^Z^V3X4Fo<]lROoRQ8kFDmUVMU?VSM4_D3Y X-Complaints-To: usenet-abuse@arcor.de Xref: x330-a1.tempe.blueboxinc.net comp.lang.javascript:10580 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