如何通过检查多个值来过滤数组/对象 [英] How to filter an array/object by checking multiple values
问题描述
我和阵列试图更了解他们,因为我倾向于和他们一起工作了很多最近玩耍。
我得到这个情况下我要搜索一个数组,并比较它的元素值,其中包含一些选定的过滤器的值另一个数组。
I'm playing around with arrays trying to understand them more since I tend to work with them alot lately. I got this case where I want to search an array and compare it's element values to another array which contains values of some selected filters.
例如,如果我选择3个滤波器,我想以后写在新的数组的比赛 - 只有那些符合所有3个滤波器,
For example if I select 3 filters, I want later to write matches in new array - only those which match all 3 filters.
有关更容易理解,我成立了一个例子在 http://jsfiddle.net/easwee/x8U4v/36/
For easier understanding I set up an example on http://jsfiddle.net/easwee/x8U4v/36/
code是:
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));
我也试过用
filtered = $.grep(workItems, function(element, index){
return element.category.indexOf(filtersArray[i]);
}, true);
但只有第一个过滤器相匹配,只有当它是在 workItems.category
我已经尝试了许多不同的解决方案,但不能真正使这项工作。我应该使用什么函数返回期望的结果?
I've tried many different solutions but can't really make this work. What function should I use to return the desired result?
推荐答案
您可以使用 .filter()
的的法阵
目标:
var filtered = workItems.filter(function(element) {
// Create an array using `.split()` method
var cats = element.category.split(' ');
// Filter the returned array based on specified filters
// If the length of the returned filtered array is equal to
// length of the filters array the element should be returned
return cats.filter(function(cat) {
return filtersArray.indexOf(cat) > -1;
}).length === filtersArray.length;
});
一些旧的浏览器,如IE8不支持阵列
对象的 .filter()
方法,如果你jQuery对象正在使用jQuery你可以使用 .filter()
方法。
Some old browsers like IE8 doesn't support .filter()
method of the Array
object, if you are using jQuery you can use .filter()
method of jQuery object.
jQuery的版本:
jQuery version:
var filtered = $(workItems).filter(function(i, element) {
var cats = element.category.split(' ');
return $(cats).filter(function(_, cat) {
return $.inArray(cat, filtersArray) > -1;
}).length === filtersArray.length;
});
这篇关于如何通过检查多个值来过滤数组/对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!