如何在2个不同的阵列应用搜索过滤器 - IONIC2
我已经应用搜索过滤器使用此代码阵列在我listproduct.ts
如何在2个不同的阵列应用搜索过滤器 - IONIC2
if (val && val.trim() != '') {
this.names = this.names.filter((names) => {
return (names.toLowerCase().indexOf(val.toLowerCase()) > -1);
})
}
请注意,是在names
阵列应用此过滤器。我希望这个过滤器也可以在catg
和pservice
命名的数组上工作。
如何在多个数组上实现过滤结果。
请指导。
您可以创建另一个bigArray
这是使用JS concat功能的三个阵列的串联:
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 !)
这里是一个工作Plunker
它没有工作,让我再试试 – user2828442
如果我将concat然后我应该写在这一行'return(names.tolowerCase ...' – user2828442
这个'return'声明仍然是一样的(见我的Plunker) – Antikhippe
如果你需要他们保持作为单独的阵列,并不能Concat的他们,你可以做这样的事情:
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);
不要我需要返回catg.tolowerCase .....,ppservice.tolowerCase ....?如果是,请更新您的答案 – user2828442
不,它只是命名不正确,应该可以工作 – user184994
而不是创建一个新变量,也可以执行'[names,catg,pservice] = [names,catg,pservice] .map (array => {...'。 –
你到底意味着什么?它应该对三个数组一次执行相同的操作吗? – Antikhippe
是的,它应该从3个数组搜索 – user2828442