adding elements to an array...

Discussion in 'Javascript' started by Frances, May 30, 2006.

  1. Frances

    Frances Guest

    how to do you add elements to an array? am looking at section dealing
    with arrays in JS Bible, can't find how you add to an array.. in the
    last few years I've been doing much more Java than JS, am used to Java,
    where you can't add elements to an array, but use a Vector instead.. and
    it's very easy to add to it (use add() method..) how to you add
    elements to an array in JavaScript..

    I looked up array obj, don't see method to add to array...

    thank you..
     
    Frances, May 30, 2006
    #1
    1. Advertising

  2. Frances wrote:

    > how to do you add elements to an array?


    There is a push method that adds its arguments to the end of the array e.g.
    var a = [0, 1, 2];
    a.push(3);
    Note that push is not supported by IE 5/Win with the JScript 5.0 engine.
    You can always do e.g.
    a[a.length] = 3;

    There are other methods to change the array, see
    <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array#Mutator_methods>


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, May 30, 2006
    #2
    1. Advertising

  3. On 30/05/2006 20:59, Vic Sowers wrote:

    [snip]

    > a[-1] = "Zero";
    > a["Zero"] = -1;


    Neither of these will be array elements.

    What constitutes an array index has been described several times (both
    by myself and others).

    See, for example:

    Author: Michael Winter
    Date: 2004-12-07 10:26:36
    Message-ID: opsimviozvx13kvk@atlantis

    <http://groups.google.co.uk/group/comp.lang.javascript/msg/a7bf8971b33f971e>

    in the thread, "Roundup of FAQ change requests".

    Mike

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.
     
    Michael Winter, May 30, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Modukuri

    Adding elements of an array

    Modukuri, May 29, 2004, in forum: VHDL
    Replies:
    4
    Views:
    621
    Christoph M. Wintersteiger
    May 30, 2004
  2. P
    Replies:
    1
    Views:
    1,201
    Joe Kesselman
    Jul 7, 2006
  3. JyotiR
    Replies:
    4
    Views:
    364
  4. justineee
    Replies:
    4
    Views:
    277
    Arne Vajhøj
    Nov 1, 2008
  5. Ari Brown
    Replies:
    4
    Views:
    133
    Elad Meidar
    Aug 30, 2007
Loading...

Share This Page