Cómo filtrar una matriz / objeto comprobando múltiples valores
Estoy jugando con matrices tratando de entenderlas más ya que tiendo a trabajar con ellas últimamente. Tengo este caso donde quiero buscar una matriz y comparar sus valores de elementos con otra matriz que contiene valores de algunos filtros seleccionados.
Por ejemplo, si selecciono 3 filtros, luego deseo escribir coincidencias en una nueva matriz, solo aquellas que coincidan con los 3 filtros.
Para una comprensión más fácil pongo un ejemplo enhttp://jsfiddle.net/easwee/x8U4v/36/
El código es:
var workItems = [
{ "id": 2616, "category": ".category-copy .category-beauty .category-fashion"}, //this is a match
{ "id": 1505, "category": ".category-beauty"}, // NOT
{ "id": 1500, "category": ".category-beauty .category-fashion"}, // NOT
{ "id": 692, "category": ".category-stills .category-retouching"}, // NOT
{ "id": 593, "category": ".category-beauty .category-capture .category-fashion .category-product .category-stills .category-stills-retouching "}, // NOT
{ "id": 636, "category": ".category-beauty .category-copy .category-fashion"}, //this is a match
{ "id": 547, "category": ".category-fashion .category-lifestyle .category-stills .category-stills-retouching "}, // NOT
{ "id": 588, "category": ".category-capture .category-recent-work .category-copy .category-beauty .category-fashion"} //this is a match
];
var filtersArray = [".category-beauty", ".category-fashion", ".category-copy"];
var i;
for (i = 0; i < filtersArray.length; ++i) {
var searchString = filtersArray[i];
console.log('Searching for: ' + searchString);
var filtered = $(workItems).filter(function(){
return this.category.indexOf(searchString);
});
}
console.log('Filtered results: ' + JSON.stringify(filtered, null, 4));
Yo también probé con
filtered = $.grep(workItems, function(element, index){
return element.category.indexOf(filtersArray[i]);
}, true);
pero coincide solo con el primer filtro y solo si es al principio deworkItems.category
He intentado muchas soluciones diferentes pero realmente no puedo hacer que esto funcione. ¿Qué función debo usar para devolver el resultado deseado?