I'm in the process of adding a sorting feature to my ionic/angular app. The app pulls vouchers from an API, each belonging to different categories. I need the system to be flexible enough to support multiple categories per voucher. Currently, the solution I've found works only if a voucher has either 0 or 2 categories - it doesn't handle single category vouchers.
The current setup filters out vouchers with no categories first, and then checks if the voucher has one or two categories by referencing the array indexes. This method is causing errors when there's only one category assigned to a voucher.
//Remove vouchers without any category
this.removeNoCats = from(this.searchvouchers).pipe(filter(item=> item.get_categories.length !== 0))
this.removeNoCats.subscribe(res => console.log(res.name));
//Filter vouchers based on categories
this.filteredList = from(this.removeNoCats)
.pipe(filter((item:any) => item.get_categories[0].name === this.category || item.get_categories[1].name === this.category),toArray())
I'm looking for a way to make the sorting feature more adaptable to handle any number of categories associated with a voucher. Any suggestions on how I can achieve this?