SHARE:

Author: Posted on November 17, 2011 at 5:54 pm
Flex 3, Flex 3
TUTORIALS
0

Flex 3: Compare Two Array Collections

I have come across frequent scenarios where I have needed to compare two different ArrayCollections and then perform some form of action depending on what is found. This simple example below shows you how to compare two ArrayCollections, in this example we are comparing ArrayCollection one, to ArrayCollection two and if ArrayCollection two has the same value (in this case name), we then remove it. The code example is well commented and should be simple to understand. If you have any questions on how to compare ArrayCollection’s please leave a comment.
[Bindable] private var collectionOne:ArrayCollection = new ArrayCollection([
	{name: “John Fitzpatrick”, number: “07856352711”},
{name: “Jane Doe”, number: “07785443622”},
{name: “Keith Doe”, number: “07778864567”},
{name: “James Willows”, number: “0789993321”},
]);
[Bindable] private var collectionTwo:ArrayCollection = new ArrayCollection([
	{name: “John Wilkinson”, number: “07856352711”},
{name: “Jane Summers”, number: “07785443622”},
{name: “Keith Doe”, number: “07778864567”},
{name: “Michael Johnson”, number: “0789993321”},
]);

//Loop through the first collection
for(var a:String in collectionOne) {
		//For each string within the first loop through the second
		for(var b:String in collectionTwo) {
//Check to see if the strings are the same
			if(collectionOne[a].name == collectionTwo[b].name) {
				//If they are, do something usefull
				collectionTwo.removeItemAt(int(b)); //Convert string to int
			}
		}
	}
  • /li>

  • REVIEWS

  • No categories have been added yet

TABLE OF CONTENTS