我已经在我的listproduct.ts
中使用此代码对数组应用了搜索过滤器
if (val && val.trim() != '') {
this.names = this.names.filter((names) => {
return (names.toLowerCase().indexOf(val.toLowerCase()) > -1);
})
}
请注意,此筛选器应用于names
数组。我希望这个过滤器也应该适用于catg
和pservice
命名数组。
如何在多个阵列上获得过滤结果?
您可以使用 js concat 函数创建另一个bigArray
即三个数组的连接:
var bigArray = this.names.concat(this.catg, this.pservice);
并在该bigArray
上调用过滤器。
var val = "test";
this.names = ["abc", "test123", "test"];
this.catg = ["qsd", "azetest"];
this.pservice = ["another test !", "tss"];
this.bigArray = this.names.concat(this.catg, this.pservice);
// here, bigArray contains all values of the three arrays (abc,test123,test,qsd,azetest,another test !,tss)
if (val && val.trim() != '') {
this.bigArray = this.bigArray.filter((names) => {
return (names.toLowerCase().indexOf(val.toLowerCase()) > -1);
})
// here, bigArray contains only filtered values of the three arrays (test123,test,azetest,another test !)
这是一个工作的普伦克
如果您需要将它们保留为单独的数组,并且无法连接它们,则可以执行以下操作:
let names = ["Andy", "Alex", "Corbyn", "Eric" ];
let catg = ["Adventure", "Action", "Comedy", "SciFi"];
let pservice = ["Example", "Service", "Allo"]
let val = "a";
[names, catg, pservice] = [names, catg, pservice].map(array => {
return array.filter((item) => {
return (item.toLowerCase().indexOf(val.toLowerCase()) > -1);
})
});
console.log(names);
console.log(catg);
console.log(pservice);