如何在2个不同的阵列上应用搜索过滤器 - IONIC2

时间:2017-11-11 09:08:15

标签: arrays angular ionic-framework ionic2

我在listproduct.ts

中使用此代码在数组上应用了搜索过滤器
if (val && val.trim() != '') {
  this.names = this.names.filter((names) => {
    return (names.toLowerCase().indexOf(val.toLowerCase()) > -1);
  })
}

请注意,此过滤器适用于names阵列。我希望此过滤器也适用于catgpservice命名数组。

  

如何在多个阵列上实现过滤结果。

请指导。

2 个答案:

答案 0 :(得分:0)

您可以使用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 !)

这是一个有效的Plunker

答案 1 :(得分:0)

如果你需要将它们作为单独的数组保存,并且无法连接它们,你可以这样做:

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);