I am trying to implement a pipe which filters a nested array. The following is my code and I have tried the solutions given on Stack Overflow but didn't succeed.
HTML Template:
<div>
<div class="card-header" *ngFor="let acc of abb | search: searchTerm; let i = index">
{{acc.gname}}
<ng-container *ngFor="let ret of acc.desc; let j = index">
<div class="card-body">
{{ret.id}} - {{ret.ename}}
</div>
</ng-container>
</div>
</div>
Component Code:
this.abb = [
{gname:'gname3', desc:[{id:123, ename:'Australia'}, {id:456, ename:'Austria'}], gid:7777},
{gname:'gname1', desc:[{id:899, ename:'France'}], gid:8876},
{gname:'gname2', desc:[{id:676, ename:'Germany'}], gid:0993}
];
SearchPipe:
export class SearchPipe implements PipeTransform {
transform(value: any[], searchName: string, key = 'gname'): any[] {
const products: any = [];
value.forEach(product => {
const matches = product[key].filter(({ name }) => name === searchName);
if (matches.length) {
products.push({ ...product, [key]: matches });
}
})
return products;
}
}
I am also getting an error in the line
const matches = product[key].filter(({ name }) => name === searchName)
which says Binding element 'name' implicitly has an 'any' type.ts(7031).
I want to filter gname
and ename
when the user types in the searchbox. Other filter implementations give me only gname
but not ename
. Any quick solution would be very appreciated.