I am facing an issue with using the filter pipe to filter my data as it is returning an empty page. Interestingly, when I remove the | filter
from the HTML code, the data appears as expected. The filter pipe should display all names if the name
exists.
This is my first time working with the filter pipe, so please let me know if there are any mistakes in the implementation.
Data Sample
[
{
"name": "Alien",
"age": 18,
"address": "lorem ipsum"
},
{
"name": "Peter",
"age": 17,
"address": "lorem ipsum"
},
{
"name": "Ben",
"age": 20,
"address": "lorem ipsum"
}
HTML Code
<ion-item *ngFor="let list of this.data | filter: 'name'">
<h2>{{ list.name }}</h2>
</ion-item>
Filter Pipe Logic
export class MyPipe implements PipeTransform {
transform(listArray: any, value: any): any {
if (value === undefined) return listArray;
for (let i = 0; i < listArray.length; i++) {
if (listArray[i].indexOf(value) !== -1) {
return value;
}
}
}
}