2 Replies Latest reply on Feb 25, 2010 9:35 AM by archemedia

    ArrayCollection - Summing values

    mfriasv

      Hello,

       

      In my app an ArrayCollection returns values like "1", "2", "3" and so on. Now, I would like to sum up 10 to each one so that the end result would be "10", "20", "30".

       

      How can I do that. I tried to find a way to convert them to numbers but I don't know if that' possible.

       

      Thanks,

      Manolo

        • 1. Re: ArrayCollection - Summing values
          Gregory Lafrance Level 6

          This answers your question:

           

          <?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 var ac:ArrayCollection = new ArrayCollection(["1", "2", "3", "4", "5", "6", "7"]);
                [Bindable] private var ac2:ArrayCollection = new ArrayCollection();
                private function init():void{
                  for each(var str:String in ac){
                    ac2.addItem(Number(str) * 10);
                  }
                }
              ]]>
            </mx:Script>
            <mx:ComboBox dataProvider="{ac2}"
              rowCount="{ac2.length}"/>
          </mx:Application>
          


          If this post answers your question or helps, please mark it as such.


          Greg Lafrance - Flex 2 and 3 ACE certified

          www.ChikaraDev.com

          Flex / AIR Development, Training, and Support Services

          • 2. Re: ArrayCollection - Summing values
            archemedia Level 4

            The answer of Greg is correct. Also, look at the special Array functions which are very powerful:

             

            I converted the base array to numbers for convenience

             

            private var ac:ArrayCollection = new ArrayCollection([1, 2, 3, 4, 5, 6, 7]);

             

            And then:

             

            private function addToCollection(amt:int):void

            {

                 ac.source.forEach(function callback(item:*, index:int, array:Array):void

                      {

                           item += amt;

                      })

                 ac.refresh();

            }

             

            Dany