Filtering carArray with a certain conditions from user.
When user checked red checkbox, it will filter cars with red paint.
When user checked green checkbox, it will filter cars with green paint. When user checked both red and green checkbox, it will show both red and green cars. (and so on with N user conditions)
I am using 2 check boxes for this example. I have more than 5 check boxes in my real implementation.
I started with showRed, showGreen boolean vars to track what users wants and an array of car object.
[ {
carName: xxx,
color: 'red'
},
{
carName: yyy,
color: 'green'
},
.....
]
filteredCars = carArray.filter((car) => {
// Problem: I tried to check for showRed and
// showGreen before returning but found out that I can
// only return once in here
if (showRed) {
return car.color === 'red';
}
if (showGreen) {
return car.color === 'green';
}
});
I am currently facing some problems on filtering with multiple user conditions.