Vegeta
Vegeta

Reputation: 37

Add arraycollection to another arraycollection without any duplicates

I'm pretty new to Flex but I'm taking a few tutorials to try and get the hang of a project I'm working on.

I'm using AS3.

I currently need to just add an arraycollection (A) to an arraycollection (B) that is populating a datagrid. So when the user clicks an option on the left column, the resulting arraycollection (A) will be added to the currently displayed results in the right column.

I also needed it to filter out any duplicate entries. So if B already had a record that was in A, it wouldn't be added.

I think I can just loop through A adding each row to B by using the additem() function, but I'm not exactly sure what syntax to use the loop properly, but if there's a better suggestion, I'm certainly open to it.

Thank so much for any assistance!

Upvotes: 0

Views: 632

Answers (1)

Christophe Herreman
Christophe Herreman

Reputation: 16085

Assuming you already have populated the 2 collections, the merge code would look like this:

var collectionA:ArrayCollection;
var collectionB:ArrayCollection;

for each (var item:Object in collectionB) {
  if (!collectionA.contains(item)) {
    collectionA.addItem(item);
  }
}

where collectionA would be the collection where the unique items from collectionB would be merged into.

Upvotes: 6

Related Questions