Herms, ArrayCollection
private function findInCollection(c:ArrayCollection, findFunction:Function):Array
{
var matches : Array = c.source.filter( findFunction );
return matches;
}
private function findFunction(propertyName:String,propertyValue:*):Function
{
return function( element : *, index : int, array : Array ) : Boolean
{
return element[propertyName] == propertyValue;
}
}
with the following use
var ac:ArrayCollection=new ArrayCollection(
[{name:'Ben', id:1, age:12},
{name:'Jack', id:2, age:22},
{name:'Joe', id:3, age:17}
]
);
var searchedElements:Array=findInCollection(ac,findFunction('id',2));
it will return an array with the next object
{name:'Jack', id:2, age:22}
The disadvantage of this method is that we hard code the property name with String. This can be harmful to code maintenance.
source
share