2 Replies Latest reply: Sep 2, 2013 9:42 AM by Aaron Beall RSS

    Array Collection Compare

    akp_332

      Hi ,

       

      Question - I want to compare 2 array collection and remove the objects from the arraycollection for example ,

       

      arr and arr1 are two arraycollection .

       

       

      arr.addItem({id:1,objectName:"123.xml",rLocalOwner:"admin"});

                                              arr.addItem({id:2,objectName:"123.xml2",rLocalOwner:"admin2"});

                                              arr.addItem({id:3,objectName:"123.xml3",rLocalOwner:"admin3"});

                                              arr.addItem({id:4,objectName:"123.xml4",rLocalOwner:"admin4"});

                                              arr.addItem({id:5,objectName:"123.xml5",rLocalOwner:"admin5"});

      ----------------------------------------------------------------------------------------- -----------------------

                                              arr1.addItem({id:1,objectName:"123.xml",rLocalOwner:""});

                                              arr1.addItem({id:2,objectName:"123.xml2",rLocalOwner:""});

                                              arr1.addItem({id:3,objectName:"123.xml3",rLocalOwner:"admin3"});

                                              arr1.addItem({id:4,objectName:"123.xml4",rLocalOwner:""});

       

      I want if arr1 conatins the rLocalOwner=''' then id of that arr1 should remove that element from the arr,

       

      In this case arr should remove the element at index 0,1,3 becasue arr1 contains the null for the rLocalOwner, how can I achieve that

       

      I thought one way of for loop inside another for loop but I think this is performance degradation is there any another way ?

       

      Regards,

      Amit

        • 1. Re: Array Collection Compare
          pauland Community Member

          First of all an empty string is not the same as null.

           

          You should use two loops in case the contents of the arrays aren't in the same order.

          • 2. Re: Array Collection Compare
            Aaron Beall Community Member

            This function does what you describe, against array instead of array collection:

             

             

            function compare(source:Array, filter:Array):void {
                      // loop end to front so splicing doesn't change index
                      sourceLoop: for(var i:int = source.length; i--; ){
                                var obj:Object = source[i];
                                // loop through the filter array to find the matching object
                                for each(var f:Object in filter){
                                          if(f.id == obj.id){
                                                    if(!f.rLocalOwner){
                                                              // if the filtered object has no local owner, 
                                                              // remove it from the source array
                                                              source.splice(i, 1);
                                                    }
                                                    continue sourceLoop;
                                          }
                                }
                      }
            }
            
            
            

             

            You should be able to adapt to array collection by replacing Array/splice() with ArrayCollection/removeItemAt()

             

            You could also look into using the ArrayCollection's filterFunction

             

            -Aaron

             

            -Aaron