How to add object to arrays?

Discussion in 'Javascript' started by aliensite, Nov 24, 2003.

  1. aliensite

    aliensite Guest

    <html>
    <body>
    <script>
    // object constructor
    function names(fname,lname){
    this.fname=fname;
    this.lname=lname
    }
    // object arrays
    list = new Array()
    list[0]=new names("Dave","Smith")
    list[1]=new names("Mary","Brown")

    //add object ("Sue","Wood") to arrays
    // How?

    // list fname
    for(var i=0;i<list.length;i++){
    alert(list.fname)
    }
    </script>
    </body>
    </html>
    aliensite, Nov 24, 2003
    #1
    1. Advertising

  2. > // object constructor
    > function names(fname,lname){
    > this.fname=fname;
    > this.lname=lname
    > }
    > // object arrays
    > list = new Array()
    > list[0]=new names("Dave","Smith")
    > list[1]=new names("Mary","Brown")



    If the objects lack methods, it is better to simply use the literal object
    notation.

    var list = [
    {fname: "Dave", lname: "Smith"},
    {fname: "Mary", lname: "Brown"}];

    > //add object ("Sue","Wood") to arrays
    > // How?


    list.push({fname: "Sue", lname: "Brown"});

    http://www.JSON.org
    Douglas Crockford, Nov 24, 2003
    #2
    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. Alexandra Stehman
    Replies:
    5
    Views:
    30,604
    Chris Smith
    Jun 17, 2004
  2. Philipp
    Replies:
    21
    Views:
    1,126
    Philipp
    Jan 20, 2009
  3. john harkin
    Replies:
    1
    Views:
    776
    john harkin
    Sep 23, 2004
  4. Are Nybakk

    Object-arrays - duplicate object-references

    Are Nybakk, Apr 12, 2008, in forum: Javascript
    Replies:
    11
    Views:
    211
    Are Nybakk
    Apr 12, 2008
  5. Tuxedo
    Replies:
    65
    Views:
    634
    Scott Sauyet
    Jan 19, 2010
Loading...

Share This Page