2 Replies Latest reply on Oct 1, 2010 8:14 AM by SiHoop

    Labeling records in an array or arrayCollection

    SiHoop Level 1

      Several of the Tour de Flex examples use an array in a way that is unfamiliar to me. For example: var array:Array= [{Profit: 2000 }, {Profit: 2400 } ];

       

      I want to put my data into the array as follows:  var array1:Array= [2000,2400];

      but I still want to be able to access the 'Profit' property when I use an ArrayCollection as in the example below.

       

      So, I have 2 questions:

      First, what is the name given to populating an array as:  var array:Array= [{Profit: 2000 }, {Profit: 2400 } ];

      and

      Second, how do I label my data with 'Profit' when I add it to an array or an ArrayCollection?

       

      Thanks for your time.

       

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  creationComplete="init()" >
          <mx:Script>
              <![CDATA[
          import mx.collections.ArrayCollection;
          private function init():void{
              var array:Array= [{Profit: 2000 }, {Profit: 2400 } ];
               var ac0:ArrayCollection = new ArrayCollection(array);
               areachart.dataProvider=ac0;
               //
               var array1:Array= [2000,2400];
               var ac1:ArrayCollection = new ArrayCollection(array1);
               areachart1.dataProvider=ac1;
          }
              ]]>
          </mx:Script>
          <mx:Panel >
              <mx:AreaChart id="areachart" color="0x323232" height="100">                   
                  <mx:series>
                      <mx:AreaSeries yField="Profit"  />
                  </mx:series>
              </mx:AreaChart>       
          </mx:Panel>
          <mx:Panel >
              <mx:AreaChart id="areachart1" color="0x323232" height="100">                   
                  <mx:series>
                      <mx:AreaSeries yField="Profit"  />
                  </mx:series>
              </mx:AreaChart>       
          </mx:Panel>
      </mx:Application>

        • 1. Re: Labeling records in an array or arrayCollection
          BhaskerChari Level 4

          Hi SiHoop,

           

          You need to defenitely label it when you are assiging the data to the Array itself.

           

          var array:Array= [{Profit: 2000 }, {Profit: 2400 } ];

           

          The above one is equivalent to creating an object and pushing the data into array.

           

          So you can use the above statement as ...below

           

          var array:Array= new Array();

          var obj:Object = new Object();

           

          obj.Profit = 2000;

          array.push(obj);

          obj:Object = new Object();

          obj.Profit = 2400;

          array.push(obj);

          Actually {Profit: 2000 } represnts an Object ...in which Profit is the label(property of Object) for the value.

           

          var array:Array= [{Profit: 2000 }, {Profit: 2400 } ]; is the simplest way of initializing the Array. You need to label it before only then only you could access it with the label.

           

          Thanks,

          Bhasker

          • 2. Re: Labeling records in an array or arrayCollection
            SiHoop Level 1

            Great answer-- thank you!