PDA

View Full Version : Searching array's


zoidberg
01-24-2007, 03:36 PM
i have a horizontal list that contains 24 pictures each with a "catID" value unique to each one, so catID=1 for the first picture and catID=24 for the last one. When a user clicks on one of the pictures a variable called "catSelected" is passed the catID value of the picture. So if a user clicks on the twelve picture, "catSelected" becomes 12.

however, i have an ArrayCollection of 600 films with each film having a catID value bestowed to it. Some of the films have the same catID value because they belong to the same category. What i want to do is to be able to search the film ArrayCollection and retrieve all the films that have the same "catID" as the "catID" selected by the user that is placed in "catSelected", does anyone know how to do this??

if that is confusing, here is a step by step guide to what im trying to do:

1 - user clicks a picture and the picture's "catID" value is passed to "catSelected"
2 - a function is called that searches through the film array table that retrieve's all the films that have the same "catID" value, the function has to return ALL the records with the same "catID" value!
3 - these films are displayed in a panel or VBox and change upon the user clicking a different picture and therefore calling the function again.

Thank you for any help!

Tink
01-24-2007, 03:41 PM
loop through the ArrayCollection and if the catID matches for an element, push it into another Array. You will then have an Array of elements with matching catID's

zoidberg
01-24-2007, 06:57 PM
yeh i see, thats a good idea, two questions:

1) not sure on for loop syntax for Flex, i'm very new at the subject;
AND
2) upon calling the function a second time for when a user clicks another picture, could i clear the array so i could add different matching films?

Tink
01-24-2007, 07:14 PM
private function getMatchingIDs( id:Number )
{
// you need to defined this array as a prop in your class
//reset the array
_matchingIDs = new Array();

// you need to type this the the datatype stored inside your ArrayCollection
var item:MyCustomObject;
var numItems:int = _yourArrayCollection.length;
for( var i:int = 0; i < numItems; i++ )
{
// you need to cast this the the datatype stored inside your ArrayCollection
item = MyCustomObject( _yourArrayCollection.getItemAt( i ) );
if( item.catID == id ) _matchingIDs.push( item );
}
} _matchingIDs will then contain all the matching items