3 Replies Latest reply on Jun 14, 2007 12:56 PM by schjlatah

    Joining two (or more) ArrayCollections

    peelmaster Level 1
      I've been searching and searching on this, but I can't seem to find an answer on how to do this. Surely, someone, somewhere must have found some need to do something similar to what I'm trying to do. :-)

      I have two ArrayCollections (ac1 (field_1, field_2) and ac2 (field_3, field_4)). Both AC's are the same length. What I'd like to do is mash the fields from the two AC's together to get ac3(field_1, field_2, field_3, field_4).

      My real goal is that I need a workaround for not being able to put information from two separate dataproviders into a datagrid.

      Any help / suggestions would be greatly appreciated.

      Thanks!
        • 1. Re: Joining two (or more) ArrayCollections
          schjlatah Level 1
          Okay, here you go.

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
          <mx:Script>
          <![CDATA[
          import mx.controls.Alert;
          import mx.collections.ArrayCollection;
          [Bindable] public var ar1:ArrayCollection = new ArrayCollection();
          [Bindable] public var ar2:ArrayCollection = new ArrayCollection();
          [Bindable] public var ar3:ArrayCollection = new ArrayCollection();

          public function init ():void
          {
          ar1.addItem('a');
          ar1.addItem('b');
          ar2.addItem('c');
          ar2.addItem('d');

          for each (var str:String in ar1)
          {
          ar3.addItem ( str );
          }
          for each (str in ar2)
          {
          ar3.addItem ( str );
          }
          }
          ]]>
          </mx:Script>

          <mx:Button click="{Alert.show(ar3.toString())}" label="Button!" horizontalCenter="0" verticalCenter="0"/>

          </mx:Application>
          • 2. Joining two (or more) ArrayCollections
            peelmaster Level 1
            Ok, we're close. Thanks so much for the suggestion. Hehe, I tried applying this to my application. I don't get the output I expected. As an example, this is the behavior I'm seeing:

            (example: Assuming there are 3 rows for each field)

            When I combine the two AC's in the way you describe I get:

            ac3 =
            Field 1 / Field 2 / Field 3 / Field 4
            object / object / null / null
            object / object / null / null
            object / object / null / null
            null / null / object / object
            null / null / object / object
            null / null / object / object

            What I'm really looking for is:
            Field 1 / Field 2 / Field 3 / Field 4
            object / object / object / object
            object / object / object / object
            object / object / object / object

            I hope this makes sense. Any suggestions on how to move closer towards my goal.
            • 3. Joining two (or more) ArrayCollections
              schjlatah Level 1
              What do your add code blocks look like? Could you post them here?
              You could try modifying what I posted earlier, except where I declared str as a String, you could try type casting it as * as in, for each (var obj:* in ac1) then that should copy whatever data you have in there as an object.

              If this just isn't working for you, check out this other thread I had going about copying arrays.
              http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?catid=587&threadid=1211199