object array syntax

Discussion in 'Javascript' started by aliensite, Jun 23, 2005.

  1. aliensite

    aliensite Guest

    Can I have an array of objects without numbering?
    I have an array I frequently edit, and its a pain
    to renumber the entire array if I insert an object
    in between existing objects.

    // define object with constructor function
    function car(make, owner) {
    this.make = make
    this.owner = owner
    }
    // array object constructor
    mycar = new Array()
    mycar[0] = new car("Ford", "Ken")
    mycar[1] = new car("Chevy", "Tom")
    mycar[2] = new car("Dodge", "Jerry")
    aliensite, Jun 23, 2005
    #1
    1. Advertising

  2. On 23/06/2005 02:01, aliensite wrote:

    > Can I have an array of objects without numbering?
    > I have an array I frequently edit, and its a pain
    > to renumber the entire array if I insert an object
    > in between existing objects.


    Use an array literal:

    var myObjects = [ new Object(...),
    new Object(...),
    new Object(...) ];

    [snip]

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
    Michael Winter, Jun 23, 2005
    #2
    1. Advertising

  3. aliensite

    Chris Rohr Guest

    Michael Winter wrote:
    > On 23/06/2005 02:01, aliensite wrote:
    >
    >> Can I have an array of objects without numbering?
    >> I have an array I frequently edit, and its a pain
    >> to renumber the entire array if I insert an object
    >> in between existing objects.

    >
    >
    > Use an array literal:
    >
    > var myObjects = [ new Object(...),
    > new Object(...),
    > new Object(...) ];
    >
    > [snip]
    >
    > Mike
    >


    Use an ArrayList. It is in the java.util package.
    Chris Rohr, Jun 23, 2005
    #3
  4. aliensite

    Chris Rohr Guest

    Chris Rohr wrote:
    > Michael Winter wrote:
    >
    >> On 23/06/2005 02:01, aliensite wrote:
    >>
    >>> Can I have an array of objects without numbering?
    >>> I have an array I frequently edit, and its a pain
    >>> to renumber the entire array if I insert an object
    >>> in between existing objects.

    >>
    >>
    >>
    >> Use an array literal:
    >>
    >> var myObjects = [ new Object(...),
    >> new Object(...),
    >> new Object(...) ];
    >>
    >> [snip]
    >>
    >> Mike
    >>

    >
    > Use an ArrayList. It is in the java.util package.

    Sorry wrong list... :-(
    Chris Rohr, Jun 23, 2005
    #4
  5. aliensite

    Vic Sowers Guest

    "aliensite" <> wrote in message
    news:...
    > Can I have an array of objects without numbering?
    > I have an array I frequently edit, and its a pain
    > to renumber the entire array if I insert an object
    > in between existing objects.
    >
    > // define object with constructor function
    > function car(make, owner) {
    > this.make = make
    > this.owner = owner
    > }
    > // array object constructor
    > mycar = new Array()
    > mycar[0] = new car("Ford", "Ken")
    > mycar[1] = new car("Chevy", "Tom")
    > mycar[2] = new car("Dodge", "Jerry")
    >


    Try:
    mycar = [];
    mycar.push(new car("Ford", "Ken"));
    mycar.push(new car("Chevy", "Tom"));
    mycar.push(new car("Dodge", "Jerry"));
    Vic Sowers, Jun 23, 2005
    #5
  6. aliensite

    Randy Webb Guest

    aliensite wrote:
    > Can I have an array of objects without numbering?
    > I have an array I frequently edit, and its a pain
    > to renumber the entire array if I insert an object
    > in between existing objects.
    >
    > // define object with constructor function
    > function car(make, owner) {
    > this.make = make
    > this.owner = owner
    > }
    > // array object constructor
    > mycar = new Array()


    num = -1;

    > mycar[0] = new car("Ford", "Ken")


    mycar[num++] = .....

    > mycar[1] = new car("Chevy", "Tom")


    mycar[num++] = .....

    > mycar[2] = new car("Dodge", "Jerry")


    mycar[num++] = .....
    etc..
    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Randy Webb, Jun 23, 2005
    #6
  7. On 23/06/2005 13:57, Randy Webb wrote:

    [snip]

    > num = -1;


    [snip]

    > mycar[num++] = .....


    Don't you mean

    num = 0;
    mycar[num++] = ...;

    or

    num = -1;
    mycar[++num] = ...;

    ?

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
    Michael Winter, Jun 23, 2005
    #7
  8. aliensite wrote:
    > Can I have an array of objects without numbering?
    > I have an array I frequently edit, and its a pain
    > to renumber the entire array if I insert an object
    > in between existing objects.
    >
    > // define object with constructor function
    > function car(make, owner) {
    > this.make = make
    > this.owner = owner
    > }
    > // array object constructor
    > mycar = new Array()
    > mycar[0] = new car("Ford", "Ken")
    > mycar[1] = new car("Chevy", "Tom")
    > mycar[2] = new car("Dodge", "Jerry")


    mycar = [
    {make: "Ford", owner: "Ken"},
    {make: "Chevy", owner: "Tom"},
    {make: "Dodge", powner: "Jerry"}
    ];

    http://www.crockford.com/javascript/survey.html
    Douglas Crockford, Jun 24, 2005
    #8
  9. aliensite

    aliensite Guest

    Thanks all!
    aliensite, Jun 25, 2005
    #9
    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. Replies:
    9
    Views:
    566
    justanotherguy
    Dec 3, 2004
  2. Richard Lionheart
    Replies:
    27
    Views:
    393
    Jean-Hugues ROBERT
    May 4, 2004
  3. gabriele renzi
    Replies:
    2
    Views:
    200
    gabriele renzi
    Dec 31, 2005
  4. Ken Bloom
    Replies:
    3
    Views:
    204
  5. Good Night Moon
    Replies:
    9
    Views:
    278
    Rick DeNatale
    Jul 25, 2007
Loading...

Share This Page