object array syntax

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aliensite

    object array syntax

    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")

  • Michael Winter

    #2
    Re: object array syntax

    On 23/06/2005 02:01, aliensite wrote:
    [color=blue]
    > 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.[/color]

    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.

    Comment

    • Chris Rohr

      #3
      Re: object array syntax

      Michael Winter wrote:[color=blue]
      > On 23/06/2005 02:01, aliensite wrote:
      >[color=green]
      >> 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.[/color]
      >
      >
      > Use an array literal:
      >
      > var myObjects = [ new Object(...),
      > new Object(...),
      > new Object(...) ];
      >
      > [snip]
      >
      > Mike
      >[/color]

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

      Comment

      • Chris Rohr

        #4
        Re: object array syntax

        Chris Rohr wrote:[color=blue]
        > Michael Winter wrote:
        >[color=green]
        >> On 23/06/2005 02:01, aliensite wrote:
        >>[color=darkred]
        >>> 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.[/color]
        >>
        >>
        >>
        >> Use an array literal:
        >>
        >> var myObjects = [ new Object(...),
        >> new Object(...),
        >> new Object(...) ];
        >>
        >> [snip]
        >>
        >> Mike
        >>[/color]
        >
        > Use an ArrayList. It is in the java.util package.[/color]
        Sorry wrong list... :-(

        Comment

        • Vic Sowers

          #5
          Re: object array syntax


          "aliensite" <aliensite@exci te.com> wrote in message
          news:1119488513 .584475.85910@g 44g2000cwa.goog legroups.com...[color=blue]
          > 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")
          >[/color]

          Try:
          mycar = [];
          mycar.push(new car("Ford", "Ken"));
          mycar.push(new car("Chevy", "Tom"));
          mycar.push(new car("Dodge", "Jerry"));


          Comment

          • Randy Webb

            #6
            Re: object array syntax

            aliensite wrote:[color=blue]
            > 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()[/color]

            num = -1;
            [color=blue]
            > mycar[0] = new car("Ford", "Ken")[/color]

            mycar[num++] = .....
            [color=blue]
            > mycar[1] = new car("Chevy", "Tom")[/color]

            mycar[num++] = .....
            [color=blue]
            > mycar[2] = new car("Dodge", "Jerry")[/color]

            mycar[num++] = .....
            etc..
            --
            Randy
            comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

            Comment

            • Michael Winter

              #7
              Re: object array syntax

              On 23/06/2005 13:57, Randy Webb wrote:

              [snip]
              [color=blue]
              > num = -1;[/color]

              [snip]
              [color=blue]
              > mycar[num++] = .....[/color]

              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.

              Comment

              • Randy Webb

                #8
                Re: object array syntax

                Michael Winter wrote:[color=blue]
                > On 23/06/2005 13:57, Randy Webb wrote:
                >
                > [snip]
                >[color=green]
                >> num = -1;[/color]
                >
                >
                > [snip]
                >[color=green]
                >> mycar[num++] = .....[/color]
                >
                >
                > Don't you mean
                >
                > num = 0;
                > mycar[num++] = ...;
                >
                > or
                >
                > num = -1;
                > mycar[++num] = ...;
                >
                > ?[/color]

                Probably so. Its the effects of posting early in the morning after
                working all night <sigh>

                --
                Randy
                comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

                Comment

                • Douglas Crockford

                  #9
                  Re: object array syntax

                  aliensite wrote:[color=blue]
                  > 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")[/color]

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


                  Comment

                  • aliensite

                    #10
                    Re: object array syntax

                    Thanks all!

                    Comment

                    Working...