Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Angular Pipes Filtering on Multiple Keys
<ul>
<li *ngFor="let item of _items | filter:{ label: filterText, description: filterText } : false">{{ item.value }} - {{ item.label }} - {{ item.description }}</li>
</ul>
@Pipe({
name: 'filter'
})
export class FilterPipe implements PipeTransform {
transform(items: any, filter: any, isAnd: bool): any {
if (filter && Array.isArray(items)) {
let filterKeys = Object.keys(filter);
if (isAnd) {
return items.filter(item =>
filterKeys.reduce((memo, keyName) =>
(memo && new RegExp(filter[keyName], 'gi').test(item[keyName])) || filter[keyName] === "", true));
} else {
return items.filter(item => {
return filterKeys.some((keyName) => {
console.log(keyName);
return new RegExp(filter[keyName], 'gi').test(item[keyName]) || filter[keyName] === "";
});
});
}
} else {
return items;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.