6 Replies Latest reply on Jun 7, 2010 1:54 AM by Bogdan Manate

    Combining Array Collections

    SpaghettiCoder Level 3

      What's the best way to combine two array collections to make a 3rd array collection?

       

      loop through all the elements of AC1 and AC2 and addItem to AC3? or is there something an easier/elegant way of going about it...?

       

      AC1 bound to 1 list, AC2 is bound to another list both have different data in them but contain the same fields...

        • 1. Re: Combining Array Collections
          BhaskerChari Level 4

          Hi SpaghettiCoder,

           

          You can loop through all the elements of AC1 and AC2 and addItem to AC3. However below is the easiar method to follow:

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="combineArrayCollections()">
          <mx:Script>
            <![CDATA[
             import mx.collections.ArrayCollection;
             private var ac1:ArrayCollection = new ArrayCollection([{label:"1",data:1},{label:"2",data:2},{label:"3",data:3},{label:"4",data :4}])
             private var ac2:ArrayCollection = new ArrayCollection([{label:"5",data:5},{label:"6",data:6},{label:"7",data:7},{label:"8",data :8}])
             private var combineArrayCollection:ArrayCollection;
            
             private function combineArrayCollections():void
             {
              combineArrayCollection = new ArrayCollection((ac1.source as Array).concat((ac2.source as Array)));    
             }
            ]]>
          </mx:Script>
          </mx:Application>

           


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


          Thanks,

          Bhasker Chari

          • 2. Re: Combining Array Collections
            Bogdan Manate Level 2

            Hello,

             

            I think this is the best way to combine two array collections:

             

            public function onComplete():void

            {

            var arrCol1 :ArrayCollection = new ArrayCollection();

            var arrCol2 :ArrayCollection = new ArrayCollection();

            var arrCol3 :ArrayCollection = new ArrayCollection();

            arrCol1.addItem({ value: "val1"});

            arrCol1.addItem({ value: "val2"});

            arrCol1.addItem({ value: "val3"});

             

            arrCol2.addItem({ value: "val4"});

            arrCol2.addItem({ value: "val5"});

            arrCol2.addItem({ value: "val6"});

            arrCol3.addAll( arrCol1 );

            arrCol3.addAll( arrCol2 );

            }

            • 3. Re: Combining Array Collections
              BhaskerChari Level 4

              Hi Bogdan Manate,

               

              I dont think there is any method "addAll" for ArrayCollection in Flex3. Are you talking in Flex4....??

               

              Is there any such method in Flex4..for ArrayCollection class?

               

               

              Thanks,

              Bhasker Chari

              • 4. Re: Combining Array Collections
                SpaghettiCoder Level 3

                Yeah the addAll didn't come to mind.  I had to look it up, apparently there is one, and it works

                • 5. Re: Combining Array Collections
                  SpaghettiCoder Level 3

                  @Bogdan

                   

                  Thanks, not familar with that method, but it's there in the reference...

                   

                  It's much more elegant than looping through both AC's and adding them to the 3rd AC.

                  • 6. Re: Combining Array Collections
                    Bogdan Manate Level 2

                    The method addAll exists, but you must use at lest SDK 3.4 I think ( not sure ). I'm using 3.5 and it's included.