I have one object of list of products and I have created one array to apply filter function to get parameter from that array. Like below
var selectedProducts = ['music', 'dance']
var products = [
{
ID : 1,
name : "pro1",
category : "music"
},
{
ID : 2,
name : "pro2",
category : "yoga"
},
{
ID : 3,
name : "pro3",
category : "music"
},
{
ID : 4,
name : "pro4",
category : "dance"
},
]
function filterFunction(){
return products.filter((abc) => {
selectedProducts.forEach(function(item, index){
return abc.category == selectedProducts[index]
});
});
}
What I am trying to do is when user select any checkbox, selected values will be stored in selectedProducts[] array and then on those values filter function will be called and array will be passed as paramater with forEach method. Code works fine on each iteration of loop but at the end it return me empty array of object. Is anything wrong with my code?