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


Groups > comp.lang.javascript > #10631

Re: Set some elements of an array

Newsgroups comp.lang.javascript
Subject Re: Set some elements of an array
From "Evertjan." <exjxw.hannivoort@interxnl.net>
References <94303e95-1469-4608-b582-33dec96f7e39@p21g2000yqm.googlegroups.com> <1u41i7ttl9evaofuq75ffktd4c9lracvbg@4ax.com>
Message-ID <Xns9FE7687CD2E85eejj99@194.109.133.133> (permalink)
Date 2012-01-27 09:16 +0000

Show all headers | View raw


Gene Wirchenko wrote on 26 jan 2012 in comp.lang.javascript:

> var b1=[1,2,3,4,0,0,0,0,0,0,0,0,0,0,0,0];
> 
> or
> 
> var b1=[1,2,3,4];
> for (var i=4; i<16; i++)
>   b1[i]=0;

A variation:

b = [1,2,3,4].concat([0,0,0,0,0,0,0,0,0,0,0,0]);

document.write(b.length + ': ' + b); 
      // 16: 1,2,3,4,0,0,0,0,0,0,0,0,0,0,0,0

====================

If the added content is not all zero but repeating
concat() is a good choice:

var b=[1,2,3,4];
for (var i=4; i<16; i+=2)
   b = b.concat([9,55]);

document.write(b.length + ': ' + b); 
       // 16: 1,2,3,4,9,55,9,55,9,55,9,55,9,55,9,55


-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

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


Thread

Set some elements of an array Archos <raul.san@sent.com> - 2012-01-25 14:05 -0800
  Re: Set some elements of an array "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2012-01-25 16:30 -0800
  Re: Set some elements of an array Gene Wirchenko <genew@ocis.net> - 2012-01-25 15:54 -0800
    Re: Set some elements of an array "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-01-27 09:16 +0000
    Re: Set some elements of an array Dr J R Stockton <reply1204@merlyn.demon.co.uk> - 2012-01-27 20:39 +0000

csiph-web