Adobe Flex | ActionScript | How to Find an Item in ArrayCollection Without getItemIndex()
By klanguedoc
When you try to find an item in an ArrayCollection by using the getItemIndex() method, you will always get a -1 result using a label or string because this method is referencing a different object in memory than the one referenced by the string value even though they may have the same label. That said, you can search for an item in ArrayCollection without using getItemIndex() method in ActionScript. Below I present such a solution. This article walks you through how to setup your code to search for an item in an ArrayCollection using a selected item (combobox) from another ArrayCollection. In the process I will also populate multiple ArrayCollections using the same flat data source. Finally, I include some filtering methods to filter out duplicate values and also selectively filter an item from a Combobox.
To demonstrate these different techniques, I will use the following flat data (the list isn’t complete and if there are any errors, please forgive my ignorance):
var objCountry:Object = {
[region:'North America', country:'Canada'],
[region:'North America', country:'USA'],
[region:'North America', country:'Mexico'],
[region:'South America', country:'Columbia'],
[region:'South America', country:'Venezuela'],
[region:'South America', country:'Bolivia'],
[region:'South America', country:'Chile'],
[region:'South America', country:'Peru'],
[region:'South America', country:'Argentina'],
[region:'South America', country:'Brazil'],
[region:'South America', country:'Ecuador'],
[region:'South America', country:'Uruguay'],
[region:'Europe', country:'France'],
[region:'Europe', country:'Germany'],
[region:'Europe', country:'Italy'],
[region:'Europe', country:'England'],
[region:'Europe', country:'Ireland'],
[region:'Europe', country:'Iceland'],
[region:'Europe', country:'Scotland'],
[region:'Europe', country:'Netherlands'],
[region:'Europe', country:'Belgium'],
[region:'Europe', country:'Finland'],
[region:'Europe', country:'Greece'],
[region:'Europe', country:'Portugal'],
[region:'Europe', country:'Spain'],
[region:'Europe', country:'Sweden'],
[region:'Europe', country:'Hungary'],
[region:'Europe', country:'Austria'],
[region:'Europe', country:'Switzerland'],
[region:'Europe', country:'Poland'],
[region:'Europe', country:'Slovakia'],
[region:'Europe', country:'Albania'],
[region:'Europe', country:'Georgia'],
[region:'Europe', country:'Lithuania'],
[region:'Middle-East', country:'India'],
[region:'Middle-East', country:'Iraq'],
[region:'Middle-East', country:'Lebanon'],
[region:'Middle-East', country:'Israel'],
[region:'Asia', country:'Japan'],
[region:'Asia', country:'China'],
[region:'Asia', country:'Taiwan'],
[region:'Asia', country:'Philippines'],
[region:'Oceania' country:'Australia'],
[region:'Oceania', country:'New Zealand']
};
[Bindable]
var countryList:Array = new Array(objCountry);
[Bindable]
var countryArr:ArrayCollection = new ArrayCollection();
[Bindable]
var regionArr:ArrayCollection = new ArrayCollection();
//create an empty object for the filter function
[Bindable]
var keys:Object = {};
private function AssignData():void
{
countryArr.source = countryList;
regionArr.source = countryList;
var countryCombo:Combobox = new Combobox();
countryCombo.dataProvider = countryArr
countryCombo.LabelField = 'country';
var regionCombo:Combobox = new Combobox();
regionCombo.dataProvider = regionArr
regionCombo.LabelField = 'region';
//filter out the duplicate region values
regionArr.filterFunction = removedDuplicates;
regionArr.refresh();
}![]() | Amazon Price: $21.12 List Price: $39.99 |
![]() | Amazon Price: $2.68 List Price: $49.99 |
![]() | Amazon Price: $9.96 List Price: $39.99 |
![]() | Amazon Price: $24.94 List Price: $49.99 |
Next assign the ArrayCollections to two comboboxes. I do this inside the AssignData function. This function could be added to the ApplicationComplete property of the Application root element in your mxml file so to setup your ui elements once the application is loaded. Notice how I assign the same flat data to different ArrayCollections and subsequently to different comboboxes using the labelField property of the combobox. The labelField property tells the compiler which element in the ArrayCollection to display.
If you add this code and run it, you will discover that the regionCombobox will have duplicate values since there are multiple countries in the same region in the flat data. To remove this duplicates, I add a filter function to the regionArr.
![]() | Amazon Price: $58.88 List Price: $99.99 |
![]() | Amazon Price: $425.00 List Price: $1,899.00 |
![]() | Amazon Price: $89.99 List Price: $299.00 |
![]() | Amazon Price: $115.00 List Price: $149.99 |
Function to Remove Duplicates
private function removedDuplicates(item:Object):Boolean {
// the return value
var retVal:Boolean = false;
// check the items in the item Ojbect to see if it contains the value being tested
if (!keys.hasOwnProperty(item.region)) {
// if not found add the item to the object
keys[item.region] = item;
retVal = true;
}
return retVal;
}Finding values in an ArrayCollection using a value from another ArrayCollection
To find a value in the ArrayCollection, I am going to use the following SelectRegion method. This method would work well in a combobox which would be triggered on the change event.
private function SelectRegion(e:Event):void
{
//Get a reference to the item selected in the combobox
var oRegion:Object = countryArr.getItemAt(e.currentTarget.selectedIndex);
//Get the region label value
var sgStr:String = oRegion.region;
/* The following line, included to demonstrate the problem, would return -1 because oRegion.region is not the same Object as the one in the regionArr even if they have the same label */
var regionIndex:Number = regionArr.getItemIndex(oRregion.region);
//Instead I will loop through the items in the regionArr collection until the label at index --sr-- matches the label selected in the combobox. After that is is easy to identify the index number and set a second combobox.
for(var sr:int; sr < regionArr.length;sr++)
{
var ob:Object= regionArr.getItemAt(sr);
var temp:String = ob.region.toString();
trace(temp);
trace(sgStr);
//Find the matching label in the countryArr ArrayCollection
//Use the label as a reference to get the index in the regionArr
if(temp == sgStr)
{
this.cbRegion.selectedIndex = sr;
return;
}
}
}



![Adobe Photoshop Elements 9 (Win/Mac) [OLD VERSION]](http://ecx.images-amazon.com/images/I/51KOPuopJ%2BL._SL75_.jpg)

![Adobe Photoshop Lightroom 3 Student and Teacher Edition [OLD VERSION]](http://ecx.images-amazon.com/images/I/41DaU9s3jML._SL75_.jpg)

Kapil Waghe 3 months ago
Hey Man,
Thanks-a-lot.
Good job, keep it up . :)