2 Replies Latest reply on Nov 25, 2009 5:02 AM by MrFork

    Accessing ArrayCollection Field Names

    MrFork

      Hello, this may sound like a n00b questoin but, I have an array collection which looks like the below:

       

          public var topFive:ArrayCollection = new ArrayCollection([
               {Month:"GAME_Redemptions"},
               {Month:"TALLINK_Redemptions"},
               {Month:"YvesRocher_Redemptions"},
               {Month:"ETAM_Redemptions"}
               ]);

      With a loop for example, if I want to access the Value, I would type, topFive[i].month. However, if I want to access the Field name what would be the code. Im assuming it would be something like topFive[i] - but this does not return the desired output.

       

      Does anyone know how to do this? I wanted to avoid having to put the array into a datagrid and then extracting the values from there.

       

      Please help!

       

      Craig

        • 1. Re: Accessing ArrayCollection Field Names
          paul.williams Level 4

          Your ArrayCollection contains five dynamic objects. So the expression 'topFive[i]' will give you the object at index 'i', rather than that object's Month property. If you do not know the names of the properties of these object then in order to access their properties you will need a second loop:

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application
              xmlns:mx="http://www.adobe.com/2006/mxml"
              layout="vertical">
             
              <mx:Script>
                  <![CDATA[
                      import mx.collections.ArrayCollection;
                     
                      private var topFive:ArrayCollection = new ArrayCollection([
                           {Month:"GAME_Redemptions"},
                           {Month:"TALLINK_Redemptions"},
                           {Month:"YvesRocher_Redemptions"},
                           {Month:"ETAM_Redemptions"}
                           ]);
                     
                      private function doLoop() : void
                      {

                          // Loop through the objects in the collection
                          for each ( var object : Object in topFive )
                          {

                              // Loop through the properties of the current object.
                              for ( var property : String in object )
                              {

                                  // Display the name of the current property and the value it contains
                                  output.text += property + ": " + object[ property ] + "\n";
                              }
                          }   
                      }
                     
                  ]]>
              </mx:Script>
             
              <mx:Button
                  label="Do Loop"
                  click="doLoop()"/>
                 
              <mx:TextInput
                  width="200"
                  height="300"
                  id="output"/>
             
          </mx:Application>

          • 2. Re: Accessing ArrayCollection Field Names
            MrFork Level 1

            Thanks for your response, much appreciated. It works perfectly. Do you know how to delete a whole column out of an array coll

            ection?

             

            e.g.

             

                private var topFive:ArrayCollection = new ArrayCollection([
                             {Month:"GAME_Redemptions", value:10},
                             {Month:"TALLINK_Redemptions", value:10},
                             {Month:"YvesRocher_Redemptions",value:10},
                             {Month:"ETAM_Redemptions",value:10}
                             ]);

            To

             

                             {Month:"GAME_Redemptions"},
                             {Month:"TALLINK_Redemptions"},
                             {Month:"YvesRocher_Redemptions"},
                             {Month:"ETAM_Redemptions"}
                             ]