I have an Array of Objects which I want to filter depending on some search Strings. I want to create a new array from my original array which will only contain Objects which have properties equal to the Strings in the search:
var _array = new Array();
_array.push({name:"Ben",Title:"Mr",location:"UK"});
_array.push({name:"Brian",Title:"Mr",location:"USA"});
_array.push({name:"Ben",Title:"Mr",location:"USA"});
var searchQuery:Array = new Array();
searchQuery.push("Ben");
searchQuery.push("Mr");
I want the new Array to contain the first and the last Object as they both contain the Strings “Ben” and “Mr”.
Can I use the Array.filter to achieve this?
Any help appreciated.
Ah nothing quite like a good old collections problem 🙂
Although JiminP’s answer is indeed correct; it suffers from a couple of performance concerns; the biggest of which being that closures in AS3 are slow, so if you are searching over a large Array the operation could run slowly.
The following function isn’t quite as clean, but will yeild better performance over a large Array.