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):
First I declare an object objCountry and build an array countryList. I assign the array to two ArrayCollections countryArr and regionArr. I am doing this to demonstrate how to take a value from one ArrayCollection to find an index in another ArrayCollection. Also creating a flat data object and assigning it to an array will allow to reset any of the ArrayCollections using the ArrayCollection.source property to its original list once I cleared out the filter.
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.
This is fairly straightforward function. The API for filter functions require to use an object as parameter in its method signature. keys is only a temporary place holder for the unique values in the ArrayCollection. If you step through the function, you would see that the compiler steps through the filterFunction method in the ArrayCollection class and when it finds a unique it adds this value to an array using the push method. The hasOwnProperty method of the Object class checks to see if a property is defined. In our case it is used to check of the Object keys already has a specific value. If the value is found, the method returns false and doesn’t add the value to the keys Object otherwise the value is added.
Function to Remove Duplicates
Finding values in an ArrayCollection using a value from another ArrayCollection
Once the label values match up, which is only two strings, it is only a matter of locating the index number using the looping index. This is it for searching and filtering. If you more information or clarification, leave me a comment and I will respond as soon as possible. If you have any suggesting on improving this tutorial, let me know.
Kevin Languedoc (author) from Canada on February 16, 2012:
Hey Kapil Waghe,
Thanks for the kind words. I am glad that the tutorial was useful :)
Kapil Waghe on February 15, 2012:
Good job, keep it up . :)