I want to Filter an array of objects with another array of object.The logic will be used in product search by category and color etc. This is my main Array :
products: [{
id: 1,
name: 'Product 1',
category: 'Home',
skill: 'Easy',
color: 'blue',
price: 100.00
}, {
id: 2,
name: 'Product 2',
category: 'Home',
skill: 'Intermediate',
color: 'red',
price: 120.00
}, {
id: 3,
name: 'Product 3',
category: 'Office',
skill: 'Intermediate',
color: 'green',
price: 190.00
}, {
id: 4,
name: 'Product 4',
category: 'Office',
skill: 'Advanced',
color: 'blue',
price: 260.00
}, {
id: 5,
name: 'Product 5',
category: 'Warehouse',
skill: 'Advanced',
color: 'white',
price: 321.00
}, {
id: 6,
name: 'Product 6',
category: 'Farm',
skill: 'Intermediate',
color: 'red',
price: 120.00
}, {
id: 7,
name: 'Product 7',
category: 'Space',
skill: 'Advanced',
color: 'green',
price: 150.00
}, {
id: 8,
name: 'Product 8',
category: 'Bathroom',
skill: 'Easy',
color: 'black',
price: 9.00
}]
The filter I am creating on the fly like this array.
The expected result is to filter product data by multiple selected categories and colors.
I have tried the following code :
var filtered = [];
for(var arr in self.products){
for(var filter in self.selectedFilters){
if(self.products[arr].category == self.selectedFilters[filter].category && self.products[arr].color == self.selectedFilters[filter].color){
filtered.push(self.products[arr]);
}
}
}
console.log(filtered);
