Skip navigation
akp_332
Currently Being Moderated

Array Collection Compare

Aug 31, 2013 10:40 PM

Tags: #4.5

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",rLocalOwn er:"admin2"});

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

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

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

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

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

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

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

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

 

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

 
Replies
  • Currently Being Moderated
    Sep 1, 2013 11:28 AM   in reply to akp_332

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 2, 2013 9:42 AM   in reply to pauland

    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

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points