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

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

问题描述:

我已经应用搜索过滤器使用此代码阵列在我listproduct.ts如何在2个不同的阵列应用搜索过滤器 - IONIC2

if (val && val.trim() != '') { 
    this.names = this.names.filter((names) => { 
    return (names.toLowerCase().indexOf(val.toLowerCase()) > -1); 
    }) 
} 

请注意,是在names阵列应用此过滤器。我希望这个过滤器也可以在catgpservice命名的数组上工作。

如何在多个数组上实现过滤结果。

请指导。

+0

你到底意味着什么?它应该对三个数组一次执行相同的操作吗? – Antikhippe

+0

是的,它应该从3个数组搜索 – user2828442

您可以创建另一个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

+0

它没有工作,让我再试试 – user2828442

+0

如果我将concat然后我应该写在这一行'return(names.tolowerCase ...' – user2828442

+0

这个'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);

+0

不要我需要返回catg.tolowerCase .....,ppservice.tolowerCase ....?如果是,请更新您的答案 – user2828442

+0

不,它只是命名不正确,应该可以工作 – user184994

+1

而不是创建一个新变量,也可以执行'[names,catg,pservice] = [names,catg,pservice] .map (array => {...'。 –