Sorting through the carArray
based on user-specified conditions.
If a user selects the red
checkbox, only cars with red paint will be displayed.
If a user selects the green
checkbox, only cars with green paint will be displayed. If both the red
and green
checkboxes are selected, both red and green cars will be shown. (and so forth for any number of user conditions)
In this example, I am using 2 checkboxes, but in my actual implementation there are more than 5 checkboxes.
To accomplish this, I began by setting boolean variables showRed
and showGreen
to track user preferences, along with an array of car
objects.
[ {
carName: xxx,
color: 'red'
},
{
carName: yyy,
color: 'green'
},
.....
]
filteredCars = carArray.filter((car) => {
// Issue: Attempted to check for showRed and
// showGreen before returning, but can only return once here
if (showRed) {
return car.color === 'red';
}
if (showGreen) {
return car.color === 'green';
}
});
I am currently experiencing challenges with filtering based on multiple user conditions.