I am trying to filter view of products but I don't know why original array is being modified, resulting in not correct filtering...
categories is an array with objects of categories {title, products[]}
categoriesView I want it to be 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 of 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);
}
First filter with categories works correctly. When I dive into products problems appear and original array is modified.