[英]How to apply search filter on 2 different arrays in Ionic 2?
I have applied search filter on a array using this code in my listproduct.ts
我在listproduct.ts
使用此代码对数组应用了搜索过滤器
if (val && val.trim() != '') {
this.names = this.names.filter((names) => {
return (names.toLowerCase().indexOf(val.toLowerCase()) > -1);
})
}
Note that this filter is applied on names
array.请注意,此过滤器应用于names
数组。 I wish this filter should also work on catg
and pservice
named arrays too.我希望这个过滤器也适用于catg
和pservice
命名数组。
How can I achieve filter result on multiple arrays?如何在多个数组上实现过滤结果?
You can create another bigArray
which is the concatenation of your three arrays using js concat function :您可以使用 js concat函数创建另一个bigArray
,它是三个数组的串联:
var bigArray = this.names.concat(this.catg, this.pservice);
and call your filter on that bigArray
.并在该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 !)
If you need to keep them as separate arrays, and cannot concat them, you could do something like this:如果您需要将它们保存为单独的数组,并且无法连接它们,您可以执行以下操作:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.