I am building a real estate website where users can apply filters. One of the filters I want to have is whether the property has a balcony, roof terrace and a garden.
So when I have only one checkbox checked(balcony) I want to show all the properties with balconies. But when I have two checkboxes checked(e.g. garden and balcony) I want to render only the properties with these specific qualities, who have both garden and balcony.
I have a solution which is rather complex and difficult to maintain. Currently I have only 3 checkboxes, but what if I add 5more? The code would become very inefficient and everything has to be coded again.
Here is my "complex" solution:
var filters = {
balcony: false,
garden: false,
roofTerrace: false
};
const checkboxFilter = plotAreaFilter.filter((item) => {
// If only Balcony is checked
if (filters.balcony && !filters.roofTerrace & !filters.garden) {
if (item.balcony) {
return true
}
}
// If both Balcony and Roof Terrace is checked
else if (filters.balcony && filters.roofTerrace & !filters.garden) {
if (item.balcony && item.roofTerrace) {
return true
}
}
// If all three Balcony, Roof Terrace and Garden is checked
else if (filters.balcony && filters.roofTerrace & filters.garden) {
if (item.balcony && item.roofTerrace && item.garden) {
return true
}
}
// If only Roof Terrace is checked
else if (!filters.balcony && filters.roofTerrace & !filters.garden) {
if (item.roofTerrace) {
return true
}
}
// If only Garden is checked
else if (!filters.balcony && !filters.roofTerrace & filters.garden) {
if (item.garden) {
return true
}
}
// If both Roof Terrace and Garden is checked
else if (!filters.balcony && filters.roofTerrace & filters.garden) {
if (item.roofTerrace && item.garden) {
return true
}
}
// If only Balcony and Garden is checked
else if (filters.balcony && !filters.roofTerrace & filters.garden) {
if (item.balcony && item.garden) {
return true
}
} else {
return true
}
})
return checkboxFilter;
I am really hoping that there is a better solution to that