Skip to content

Instantly share code, notes, and snippets.

@wholypantalones
Last active September 11, 2018 14:33
Show Gist options
  • Save wholypantalones/ce221af6b99fb7e2b4dd121e721f7fcc to your computer and use it in GitHub Desktop.
Save wholypantalones/ce221af6b99fb7e2b4dd121e721f7fcc to your computer and use it in GitHub Desktop.
Angular 4 search pipe for an array of objects
// *ngFor="let row of (sample | searchPipe: input.value)"
@Pipe({
name: 'searchPipe',
pure: false
})
export class SearchPipe implements PipeTransform {
transform(sample: any[], searchTerm: string): any[] {
if (!searchTerm) return sample;
searchTerm = searchTerm.toLowerCase();
if (sample) {
return sample.filter(obj => {
Object.keys(obj).forEach(key => {
obj[key] = obj[key].toString().toLowerCase();
});
return Object.keys(obj).some(key => obj[key].includes(searchTerm));
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment