Reputation: 277
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.
Upvotes: 1
Views: 4199
Reputation: 6209
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.
var _array : 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"});
// I presumed you would want a little bit more control over the search matching; by
// using a Map you can ensure that no-one with the (somewhat unlikley) name of "Mr"
// gets matched by mistake.
var searchQueryMap : Dictionary = new Dictionary();
searchQueryMap["name"] = "Ben";
searchQueryMap["Title"] = "Mr";
const results : Array = [];
// Loop over earch objectin the 'haystack' that we wish to search.
for each (var object : Object in _array)
{
// This variable is used to break out of the loop if the current object doesn't match the
// searchQueryMap; this gets reset to true for each loop of the supplied array.
var match : Boolean = true;
// Loop over each key (property) in the searchQueryMap.
for (var key : * in searchQueryMap)
{
if (searchQueryMap[key] !== object[key])
{
// No match, we can break out of looping over the searchQueryMap here.
match = false;
break;
}
}
// Check to see if we still have a positive match; if we do, push it onto the results Array.
if (match) {
results.push(object);
}
}
// Debug the results.
trace("Matches:");
for each (var result : Object in results)
{
for (var prop : * in result) {
trace("\t" + prop + " => " + result[prop]);
}
trace("---");
}
Upvotes: 3
Reputation: 2142
This is my approach:
var _array: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");
var resultArray:Array = _array.filter(ff); //The result.
function ff(el:*,ind:int,arr:Array){//Filter Function
for(var i:int=0;i<searchQuery.length;i++){//Everything in searchQuery array should in el object.
var b:Boolean = false;
for(var s:String in el){
if(el[s]==searchQuery[i]){
b=true; break;
}
}
if(!b) return false; //no searchQuery[i] in el... :(
}
return true;
}
Upvotes: 3