I am working on multi filter checkbox in react and redux. How to add extra price filter logic when:
-
The price range is less than 250
-
The price range is between 251 and 450
-
The price range is greater than 450
Below is the code for the filter reducer. Tried this if else condition but the problem is if multiple checkboxes are clicked this doesn’t work
case actionTypes.FILTER_PRODUCT:
const searchParameters = data && Object.keys(Object.assign({}, ...data));
let filterData;
console.log('test',action.payload);
const searchQuery = action.payload.map((val) => val.toLowerCase());
const getItemParam = (item, parameter) =>
(item[parameter] ?? "").toString().toLowerCase();
if (action.payload.length === 0) {
filterData = state.products;
}
else if(action.payload.includes('0-Rs250')){
filterData = state.products.filter((item) => {
return item.price <= 250
})
}
else if(action.payload.includes('Rs251-450')){
data = state.products.filter((item) => {
return item.price > 250 && item.price <= 450
})
}else if(action.payload.includes('Rs 450')){
filterData = state.products.filter((item) => {
return item.price > 450
})
}
else {
filterData = state.products.filter((item) => {
return searchParameters.some((parameter) => {
const itemParam = getItemParam(item, parameter);
return searchQuery.some(
(color) => itemParam === color)
});
});
}
console.log('test1',filterData);
return {
...state,
filteredData: filterData,
};
2
Answers
You could add this logic for prices ranges like this:
(here I used normal
price
to explain how to add this logic, you can use your ownvalue
instead of thisprice
)I hope this helps you!
Change the conditions from exclusive OR to inclusive OR by changing from
if - else if - else
toif - if - if
.Example:
Start with initially empty filtered result set, and for each filtering criteria filter and append the original
state.products
array.An alternative using a more inline solution:
This second method retains the original order of products from the source array.