我想过滤一个具有多个数组字符串值的对象数组。
下面是我的数组对象就像
[{
"name": "FULLY MAINTAINED MARUTI SUZUKI SWIFT VDI 2008",
"model": "Swift"
}, {
"name": "maruti suzuki test",
"model": "Swift Desire"
}, {
"name": "maruti suzuki test2",
"model": "Alto"
}, {
"name": "maruti suzuki swift desire for sale1",
"model": "Zen"
}, {
"name": "maruti suzuki test",
"model": "Alto"
}, {
"name": "maruti suzuki test",
"model": "Zen"
}]
我正在使用字符串数组(下面)过滤这个对象数组(上面)
this.filterData.model=["Swift","Zen"]
必须与所有Swift和Zen模型一起显示。 我已经使用单值如何查找值数组。
添加了一个plunker链接 stackblitz Editor link
答案 0 :(得分:1)
您只需从filter.pipe.ts
.find(key => item[key] !== filter[key]); // this was for string comparison
致:
.find(key => filter[key].indexOf(item[key]) == -1); // this is how you can do that with array
答案 1 :(得分:1)
我在你的stackblitz测试了它,它没有处于编辑模式:(。
以下是我添加的代码和组件范围this.filteredData:any[] = [];
this.filterData.model=["Swift", "zen"];
for(let i = 0; i < this.filterData.model.length; i++){
let products = this.products.filter((product: any) =>
product.model.toLowerCase().indexOf(this.filterData.model[i].toLowerCase()) === 0);
for(let j = 0; j < products.length; j++){
this.filteredData.push(products[j]);
}
}
console.log(this.filteredData);