typescript 打字稿过滤对象数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49653822/
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-10-21 05:20:20  来源:igfitidea点击:

Typescript filter an array of objects

javascripttypescript

提问by Krishna

I want to filter my resultsarray based on the values stored in filters. I have tried the below code. But it is not applying.

我想results根据存储在过滤器中的值过滤我的数组。我试过下面的代码。但它不适用。

let filters = {
  name: ["Krishna", "Naveen"],
  city : ["London"]
};
results = [
{
    "name": "Krishna#Surname",
    "city": "London",
    "age": 23
},
{
    "name": "Naveen#Surname",
    "city": "London",
    "age": 23
},
{
    "name": "Krishna#Surname",
    "city": "NewYork",
    "age": 23
},
{
    "name": "Praveen#Surname",
    "city": "Washington",
    "age": 23
}
]
this.results1 = this.multiFilter(results,filters);

multiFilter(array:any=[], filters:Object) {
const filterKeys = Object.keys(filters);
return array.filter((item) => {
  return filterKeys.every(key => {
    let filters1= filters[key];
    return filters1.every(key1 => {
      return !!~ item[key].indexOf(key1)
    });
  });
});
}

回答by Ele

An alternative is using the function filteralong with functions includesand some.

另一种方法是将该函数filter与函数includes和一起使用some

let filters = { name: ["Krishna", "Naveen"], city : ["London"]},
    results = [{    "name": "Krishna#Surname",    "city": "London",    "age": 23},{    "name": "Naveen",    "city": "London",    "age": 23},{    "name": "Krishna",    "city": "NewYork",    "age": 23},{    "name": "Praveen",    "city": "Washington",    "age": 23}],
    result = results.filter(({name, city}) => filters.name.some(n => name.includes(n)) && filters.city.includes(city));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

回答by Nina Scholz

You could check for every filter and returns that object which matches all conditions.

您可以检查每个过滤器并返回匹配所有条件的对象。

This approach works for an arbitrary count of conditions.

这种方法适用于任意数量的条件。

function multiFilter(array, filters) {
    return array.filter(o =>
        Object.keys(filters).every(k =>
            [].concat(filters[k]).some(v => o[k].includes(v))));
}

var filters = { name: ["Krishna", "Naveen"], city: ["London"] },
    results = [{ name: "Krishna#Surname", city: "London", age: 23 }, { name: "Naveen#Surname", city: "London", age: 23 }, { name: "Krishna#Surname", city: "NewYork", age: 23 }, { name: "Praveen#Surname", city: "Washington", age: 23 }],
    filtered = multiFilter(results, filters);
  
  console.log(filtered);
.as-console-wrapper { max-height: 100% !important; top: 0; }

回答by Eddie

You can filterthe array and use includesto check if name and city is on the array

您可以filter使用数组并用于includes检查名称和城市是否在数组中

let filters = {name: ["Krishna", "Naveen"],city: ["London"]};
let results = [{"name": "Krishna","city": "London","age": 23},{"name": "Naveen","city": "London","age": 23},{"name": "Krishna","city": "NewYork","age": 23},{"name": "Praveen","city": "Washington","age": 23}]

let result = results.filter(o => filters.name.includes(o.name) && filters.city.includes(o.city));

console.log(result);