I have a angular component which has got 3 filters (category, rating & price) and when selecting those filters I have to show only the appropriate data so I have created a Pipe and passed in all three selected values. Actually my category filter works completely fine but the other two filters (rating & price) are not working properly.
Json data:
"allBusiness": [
{
"category": "XYZ",
"description": "something something",
"business": [
{
"type": "Store",
"rating": 4,
"price" : "$"
},
{
"type": "Store",
"rating": 3,
"price" : "$"
},
{
"type": "Store",
"rating": 5,
"price" : "$$"
}
]
},
{
"category": "ABC",
"description": "Testing",
"business": [
{
"type": "Online",
"rating": 3,
"price" : "$"
},
{
"type": "Store",
"rating": 2,
"price" : "$"
},
{
"type": "Store",
"rating": 1,
"price" : "$$"
}
]
}
]
FilterPipe.ts:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'filter'
})
export class FilterPipe implements PipeTransform {
transform(allBusiness: any[], selectedCategory: string, selectedRating: number, selectedPrice: string): any {
if (allBusiness && allBusiness.length){
return allBusiness.filter(item =>{
if (category && item.category.indexOf(selectedCategory) === -1){
return false;
}
if (rate && item.business.filter(rt => rt.rating != selectedRating)){ //tried filter, findIndex, some but nothing works
return false;
}
if (price && item.business.find(pr => pr.price != selectedPrice)){ //same here nothing is working
return false;
}
return true;
})
}
else{
return items;
}
}
}
Thanks in advance.