jQuery 如何通过检查多个值来过滤数组/对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18719383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 22:18:12  来源:igfitidea点击:

How to filter an array/object by checking multiple values

javascriptjqueryarrays

提问by easwee

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.

我正在尝试使用数组试图更多地理解它们,因为我最近经常与它们一起工作。我遇到了这种情况,我想搜索一个数组并将其元素值与另一个包含某些选定过滤器值的数组进行比较。

For example if I select 3 filters, I want later to write matches in new array - only those which match all 3 filters.

例如,如果我选择 3 个过滤器,我想稍后在新数组中写入匹配项 - 仅匹配所有 3 个过滤器的那些。

For easier understanding I set up an example on http://jsfiddle.net/easwee/x8U4v/36/

为了更容易理解,我在http://jsfiddle.net/easwee/x8U4v/36/上设置了一个例子

Code is:

代码是:

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

I also tried with

我也试过

filtered = $.grep(workItems, function(element, index){
    return element.category.indexOf(filtersArray[i]); 
}, true);

but it matches only the first filter and only if it's at the begining of workItems.category

但它只匹配第一个过滤器,并且只有在它的开头 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?

我尝试了许多不同的解决方案,但无法真正做到这一点。我应该使用什么函数来返回所需的结果?

采纳答案by undefined

You can use .filter()method of the Arrayobject:

您可以使用对象的.filter()方法Array

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

http://jsfiddle.net/6RBnB/

http://jsfiddle.net/6RBnB/

Some old browsers like IE8 doesn't support .filter()method of the Arrayobject, if you are using jQuery you can use .filter()method of jQuery object.

一些老浏览器如IE8不支持对象的.filter()方法Array,如果你使用jQuery,你可以使用.filter()jQuery对象的方法。

jQuery version:

jQuery 版本:

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

回答by aabiro

You can use .filter() with boolean operators ie &&:

您可以将 .filter() 与布尔运算符一起使用,即 &&:

 var find = my_array.filter(function(result) {
       return result.param1 === "srting1" && result.param2 === 'string2';
       });
    return find[0];
 }