Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #10582 > unrolled thread
| Started by | Archos <raul.san@sent.com> |
|---|---|
| First post | 2012-01-25 14:05 -0800 |
| Last post | 2012-01-27 20:39 +0000 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.javascript
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
| From | Archos <raul.san@sent.com> |
|---|---|
| Date | 2012-01-25 14:05 -0800 |
| Subject | Set some elements of an array |
| Message-ID | <94303e95-1469-4608-b582-33dec96f7e39@p21g2000yqm.googlegroups.com> |
I want to initialize an array of 16 elemts to 0
var b1 = []; for (var i=0; i<16; i++){ b1[i]=0; }
But, then, I want that the 4 first elements have a value, so I tried
this one:
b1 = [1, 2, 3, 4 , b1[4:16]]
althought it doesn't works. How to get it?
Thanks in advance
[toc] | [next] | [standalone]
| From | "Michael Haufe (TNO)" <tno@thenewobjective.com> |
|---|---|
| Date | 2012-01-25 16:30 -0800 |
| Message-ID | <5bef5abc-0dde-43b4-b244-ca023081f871@t8g2000yqg.googlegroups.com> |
| In reply to | #10582 |
On Jan 25, 4:05 pm, Archos <raul....@sent.com> wrote:
> I want to initialize an array of 16 elemts to 0
>
> var b1 = []; for (var i=0; i<16; i++){ b1[i]=0; }
>
> But, then, I want that the 4 first elements have a value, so I tried
> this one:
>
> b1 = [1, 2, 3, 4 , b1[4:16]]
>
> althought it doesn't works. How to get it?
> Thanks in advance
JavaScript doesn't use slice syntax. It was proposed at some point,
but never followed up on AFAIK.
b1 = [1,2,3,4].concat(b1.slice(4))
[toc] | [prev] | [next] | [standalone]
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2012-01-25 15:54 -0800 |
| Message-ID | <1u41i7ttl9evaofuq75ffktd4c9lracvbg@4ax.com> |
| In reply to | #10582 |
On Wed, 25 Jan 2012 14:05:07 -0800 (PST), Archos <raul.san@sent.com>
wrote:
>I want to initialize an array of 16 elemts to 0
>
> var b1 = []; for (var i=0; i<16; i++){ b1[i]=0; }
>
>But, then, I want that the 4 first elements have a value, so I tried
>this one:
>
> b1 = [1, 2, 3, 4 , b1[4:16]]
>
>althought it doesn't works. How to get it?
>Thanks in advance
Try
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;
Are you thinking that an array is fixed in size at declaration?
It is not.
Sincerely,
Gene Wirchenko
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exjxw.hannivoort@interxnl.net> |
|---|---|
| Date | 2012-01-27 09:16 +0000 |
| Message-ID | <Xns9FE7687CD2E85eejj99@194.109.133.133> |
| In reply to | #10610 |
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)
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <reply1204@merlyn.demon.co.uk> |
|---|---|
| Date | 2012-01-27 20:39 +0000 |
| Message-ID | <xynDFTDEuwIPFwCs@invalid.uk.co.demon.merlyn.invalid> |
| In reply to | #10610 |
In comp.lang.javascript message <1u41i7ttl9evaofuq75ffktd4c9lracvbg@4ax.
com>, Wed, 25 Jan 2012 15:54:35, Gene Wirchenko <genew@ocis.net> posted:
> Try
>
>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;
Try
var b1=[1,2,3,4,0,0,0,0,0,0,0,0,0,0,0,0];
or
var b1=[], i=0
do { b1[i] = ++i * (i<5) } while (i < 16)
NOTE - it is well to avoid i & l & o & O for variable identifiers in
News articles, since you know nothing about the monitors, fonts,
spelling-checkers, and eyeballs that your avid readers are using.
--
(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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web