Set some elements of an array

Discussion in 'Javascript' started by Archos, Jan 25, 2012.

  1. Archos

    Archos Guest

    I want to initialize an array of 16 elemts to 0

    var b1 = []; for (var i=0; i<16; i++){ b1=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
     
    Archos, Jan 25, 2012
    #1
    1. Advertisements

  2. On Wed, 25 Jan 2012 14:05:07 -0800 (PST), Archos <>
    wrote:

    >I want to initialize an array of 16 elemts to 0
    >
    > var b1 = []; for (var i=0; i<16; i++){ b1=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=0;

    Are you thinking that an array is fixed in size at declaration?
    It is not.

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Jan 25, 2012
    #2
    1. Advertisements

  3. On Jan 25, 4:05 pm, Archos <> wrote:
    > I want to initialize an array of 16 elemts to 0
    >
    >     var b1 = []; for (var i=0; i<16; i++){ b1=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))
     
    Michael Haufe (TNO), Jan 26, 2012
    #3
  4. Archos

    Evertjan. Guest

    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=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)
     
    Evertjan., Jan 27, 2012
    #4
  5. In comp.lang.javascript message <1u41i7ttl9evaofuq75ffktd4c9lracvbg@4ax.
    com>, Wed, 25 Jan 2012 15:54:35, Gene Wirchenko <> 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=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<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.
     
    Dr J R Stockton, Jan 27, 2012
    #5
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Harald Kirsch
    Replies:
    4
    Views:
    3,183
    Harald Kirsch
    Aug 31, 2004
  2. P
    Replies:
    1
    Views:
    1,486
    Joe Kesselman
    Jul 7, 2006
  3. Jason Heyes
    Replies:
    11
    Views:
    1,175
    Maxim Yegorushkin
    Jan 16, 2006
  4. Replies:
    3
    Views:
    619
    Joseph Kesselman
    May 23, 2007
  5. Replies:
    2
    Views:
    628
    Boris Kolpackov
    May 23, 2007
  6. Replies:
    1
    Views:
    606
    Boris Kolpackov
    May 24, 2007
  7. Martin Kissner

    randomly choose some uniq elements of an array

    Martin Kissner, Jan 19, 2006, in forum: Perl Misc
    Replies:
    18
    Views:
    343
    Anno Siegel
    Jan 23, 2006
  8. Replies:
    1
    Views:
    342
Loading...