This is my demo code:
//Imports and decorators up here for Angular 2 export class ProductsListComponent { products = [ { name: "A", color: "Blue", size: 50 }, { name: "B", color: "Blue", size: 60 }, { name: "C", color: "Black", size: 70 } ]; filters = { colors: ["Blue", "Black"], sizes: [70, 50] }; //This is my first approach but just works for the colors array inside filters object //and i have no ideia how to filter sizes too filterProducts() { let results = []; this.filters.colors.forEach((color) => { this.products.filter((product) => { if (product.color === color) { results.push(product); } return true; //filter callback requires a boolean }) }); console.log(results); } }
I would like the results array to look like this: filters :
var results = [ { "name": "A", "color": "Blue", "size": 50 }, { "name": "C", "color": "Black", "size": 70 } ];
I hope to explain my problem well.
Elkin source share