4 Replies Latest reply on Jul 21, 2008 8:52 AM by Rothrock

    question about arrays

    Level 7
      I have tried the following code and get some rather weird results! I wonder
      if anyone could shed some light.

      aARRAY=new Array();

      aTEMP_ARRAY=new Array();

      aTEMP_ARRAY.id="1";
      aTEMP_ARRAY.desc="hello";

      aARRAY.push(aTEMP_ARRAY);

      //aTEMP_ARRAY=new Array();
      aTEMP_ARRAY.desc="goodbye";

      With the line commented out as above, the value of aARRAY[0].desc changes to
      "goodbye", but with the comment removed the value of aARRAY[0].desc stays as
      "hello"

      Could anyone please explain this, it is causing us some headaches. What
      would be the correct method to do this. Basically we have a master array
      "aARRAY" which holds other property arrays.

      Many thanks


        • 1. Re: question about arrays
          Rothrock Level 5
          I'm sure it must have something to do with Flash passing references to arrays and not actually making copies. But my first question would be why do you want to do this anyways?

          Instead of making and array and then adding properties to it, why don't you make an object and push that onto your master array? (Also aren't all caps usually reserved for constants? And why would you make ids, that look like numbers but are strings? Just sayin'....)

          So:

          masterArray.push({id:1,des:"hello"});
          • 2. Re: question about arrays
            Level 7
            First of all, you should not use the Array class like this:

            aTEMP_ARRAY.id="1";

            There is no 'property array' such as in Director. Instead you should use the
            Object class, which is for associative arrays, and push objects onto an
            indexed array.

            And right - it's what Rothrock said - arrays are assigned by reference,
            rather than value - this is true in most languages.

            --
            Dave -
            www.offroadfire.com
            Head Developer
            http://www.blurredistinction.com
            Adobe Community Expert
            http://www.adobe.com/communities/experts/


            • 3. Re: question about arrays
              Level 7
              Thanks for the reply, I think I was having an "off" day as I uisually use
              objects. However, I still get the same behaviour if I update the object
              after I have pushed it into the array. With the code as below the
              aWAYPOIINTS array STILL conatains the updated description property. Why is
              that?

              Thanks for any further advice.

              aWAYPOINTS=new Array();

              var objTEMP_WAYPOINT=new Object();
              objTEMP_WAYPOINT.id=0;
              objTEMP_WAYPOINT.latitude=0;
              objTEMP_WAYPOINT.longitude=0;
              objTEMP_WAYPOINT.direction=0;
              //objTEMP_WAYPOINT.description="[empty]";
              aWAYPOINTS.push(objTEMP_WAYPOINT);

              objTEMP_WAYPOINT.description="[empty]";



              "Rothrock" <webforumsuser@macromedia.com> wrote in message
              news:g5o46f$9cm$1@forums.macromedia.com...
              > I'm sure it must have something to do with Flash passing references to
              > arrays
              > and not actually making copies. But my first question would be why do you
              > want
              > to do this anyways?
              >
              > Instead of making and array and then adding properties to it, why don't
              > you
              > make an object and push that onto your master array? (Also aren't all caps
              > usually reserved for constants? And why would you make ids, that look like
              > numbers but are strings? Just sayin'....)
              >
              > So:
              >
              > masterArray.push({id:1,des:"hello"});
              >


              • 4. Re: question about arrays
                Rothrock Level 5
                That would be because of the reference vs. value issue.

                Create a new object each time.