2011-05-11 6 views
0

J'ai un tableau d'objets que je veux filtrer en fonction de certaines chaînes de recherche. Je veux créer un nouveau tableau de mon tableau original qui ne contiennent des objets qui ont des propriétés égales aux chaînes dans la recherche:AS3 Filter Array of Object

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"); 

Je veux que le nouveau tableau pour contenir le premier et le dernier objet que les deux contiennent les cordes "Ben" et "Mr". Puis-je utiliser le tableau Array.filter pour réaliser cela?

Toute aide appréciée.

Répondre

2

rien Ah tout à fait comme un bon vieux problème collections :)

Bien que la réponse de JiminP est en effet correcte; il souffre de quelques problèmes de performance; la plus grande étant closures in AS3 are slow, donc si vous cherchez sur un grand tableau, l'opération pourrait être lente.

La fonction suivante n'est pas tout à fait aussi propre, mais vous obtiendrez de meilleures performances sur une grande baie.

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("---"); 
} 
+0

Parfaitement merci! – redHouse71

3

Ceci est mon approche:

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; 
}