Ionic 3管道通过比较2个数组来过滤列表 [英] Ionic 3 pipe to filter list by comparing 2 arrays
本文介绍了Ionic 3管道通过比较2个数组来过滤列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要编写过滤器PIPE组件的帮助,该组件将仅过滤和显示包含所选搜索数组词的职位. 找到了一种使用1个搜索值返回的方法,但我需要它在数组中返回多个搜索结果
I need help to write a filter PIPE component that will filter and display only the job posts that contains the selected search array words. Found a way to return by using 1 search value but i need it to return multiple search result in an array
jobCategory=['admin','clerk','driver','labour','helper']
selectedCategory=['driver','helper']
HTML:
<ion-item *ngFor="let posting of (postingList | postingFilter:'jobCategory':selectedCategory)" >
PIPE:
export class PostingFilterPipe implements PipeTransform {
transform(items: any[], field : string, value : string): any[] {
if (!items) return [];
if (!value || value.length == 0) return items;
return items.filter(it => {
for (let index = 0; index < value.length; index++) {
const element = value[index]; ***STUCK***
}
});
}
}
推荐答案
您可以尝试以下方法:
export class PostingFilterPipe implements PipeTransform {
transform(items, field : string, value): any[] {
if (!items) return [];
if (!value || value.length == 0) return items;
return items.filter(it => value.filter(val => it[field].includes(val)).length > 0)
});
}
这篇关于Ionic 3管道通过比较2个数组来过滤列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文