8 Replies Latest reply on Nov 11, 2011 3:49 AM by jfb00

    Combine two arrays

    jfb00 Level 3

      Hi,

      i got this link from Alex about merging two list

      http://blogs.adobe.com/aharui/2008/03/custom_ilists_checkboxdatagrid_1.html

      I got to the point of this line:

      // merge the two arrays

      mergedIList = new MergedIList(arrayCollection1, arrayCollection2);

       

      I don't understand the next line:

      // set the function that merges the two arrays into a new extended ValueObject

      mergedIList.factoryFunction = mergeFunction;

       

      Both of my AC's have a combination of columns the same that I want to check and if they are the same i would like to combine in one AC.

      Anybody here can help me to learn about iList.??

      Thanks!

        • 1. Re: Combine two arrays
          Flex harUI Adobe Employee

          I’m not quite sure what you are looking for so maybe show 3 data objects of each list and what you want the result list to look like

          • 2. Re: Combine two arrays
            jfb00 Level 3

            Hi Alex,

            Here are the two arrayCollections where the common columns are Key01_id and key02_id:

             

            [Bindable] private var acSample01:ArrayCollection = new ArrayCollection([
                            {key01_id:100, key02_id:120, Fname: "Kranthi", Lname:"Kata"},
                            {key01_id:101, key02_id:121, Fname: "Vasanth", Lname:"Lola"}
                        ]);

             

            [Bindable] private var acSample02:ArrayCollection = new ArrayCollection([
                             {key01_id:100, key02_id:120, dob:"21/10/1972", Amount:10000, Rate:20},
                             {key01_id:101, key02_id:121, dob:"1/9/1971", Amount:3000, Rate:10}
                         ]);

             

            How can I combine this two in another array collection like:

            [Bindable] private var acSample:ArrayCollection = new ArrayCollection([
                             {key01_id:100, key02_id:120, Fname: "Kranthi", Lname:"Kata", dob:"21/10/1972", Amount:10000, Rate:20},
                             {key01_id:101, key02_id:121, Fname: "Vasanth", Lname:"Lola", dob:"1/9/1971", Amount:3000, Rate:10}
                         ]);

            In your example I can see the two arrays inside the Ilist but not the combination.

            Thanks

            • 3. Re: Combine two arrays
              Flex harUI Adobe Employee

              Is your goal to also filter out items for which there aren’t matches in the key fields?

              • 4. Re: Combine two arrays
                jfb00 Level 3

                Not now, all records in both arrays match the primary keys.

                Thanks!

                • 5. Re: Combine two arrays
                  Flex harUI Adobe Employee

                  The merged function would look like:

                   

                  Private function mergeFunction(obj1:Object, obj2:Object):*

                  {

                      var newObject:Object = {};

                      newObject.key01_id = obj1.key01_id;

                      newObject.key02_id = obj1.key02_id;

                      newObject.FName = obj1.FName;

                      newObject.LName = obj1.LName;

                      newObject.dob = obj2.dob;

                      newObject.Amount = obj2.Amount;

                      newObject.Rate = obj2.Rate;

                      return newObject;

                  }

                   

                  You may have to switch obj1 and obj2 depending on your setup.

                  • 6. Re: Combine two arrays
                    jfb00 Level 3

                    Nice Alex,

                    I am going to try to build this object dynamically since i have so many columns.

                    Thanks!

                    • 7. Re: Combine two arrays
                      Flex harUI Adobe Employee

                      To build dynamically, you can use for..in loops.  I would recommend writing a data class to hold the merged data.  Property access will be slightly faster and the compiler and runtime will find mistakes.

                       

                      Private function mergeFunction(obj1:Object, obj2:Object):*

                      {

                          var newObject:MergedDataClass = new MergedDataClass();

                          for (var p:String in obj1)

                              newObject[p] = obj1[p];

                          for (var p:String in obj2)

                              newObject[p] = obj2[p];

                       

                          return newObject;

                       

                      • 8. Re: Combine two arrays
                        jfb00 Level 3

                        Excellent Alex,

                        Thanks for your help!

                        Best