Attempting to refine the display of products, I noticed a problem where the original array is unintentionally altered, leading to incorrect filtering...
categories consists of an array containing objects with categories {title, products[]}
I aim for categoriesView to serve as a filtered array
filterByKeyword(keyword: string) {
let k = keyword.toLowerCase();
this.categoriesView = this.categories.filter((c) => {
for(let q = 0; q < c.products.length; q++) {
return c.products[q].title.toLowerCase().indexOf(k) >= 0;
}
});
// Filter products within each category:
for(let q = 0; q < this.categoriesView.length; q++) {
for(let z = 0; z < this.categories.length; z++) {
if(this.categoriesView[q].title == this.categories[z].title){
this.categoriesView[q].products = this.categories[z].products.filter((p) => {
return p.title.toLowerCase().indexOf(k) >= 0;
});
}
}
}
console.log("Categories View: ", this.categoriesView);
console.log("Categories: ", this.categories);
}
The initial filter operation on categories functions correctly. However, when delving into products, issues arise and changes are made to the original array.