I am creating a component in my app that lists all products being sold. I am then creating a filtering component where the user can open a modal with many checkboxes. They select the different options they want to filter by, and the list of products automatically updates.
Each item that is selected goes into a new array called 'selected items', which is automatically updated each time a checkbox is clicked.
Currently, I have the ability to create a single static filter like:
var A = products.filter(e => e.categories.find(category => category.slug === "movies"));
function showA() {
setfilteredProducts(A)
}
or with two parameters like:
var B = products.filter(e => e.categories.find(category => category.slug === "movies") && e.categories.find(category => category.slug === "books"));
function showB() {
setfilteredProducts(B)
}
But I am looking for a way to modify the filter function so that it will filter based on all items that are selected. So instead of writing e.categories.find(category => category.slug === "movies") && e.categories.find(category => category.slug === "books")
like in the var B, it would loop through my array of selected items and create e.categories.find(category => category.slug === "movies") &&
as many times as needed.
I am a little bit confused on how to get this to work. I may not be explaining it very clearly, so I can give more detail if necessary. But if it is enough info, any help would be appreciated.
Update:
the array I have that holds the list of selected items is:
const newArr = state.theme.filters.categories.flatMap(category => category.data).filter(item => item.checked);