我不知道如何过滤具有多个条件的数组。我有一个搜索过滤器表单,其中包含 2 个选择、1 个复选框字段集和 1 个单选按钮字段集。我有返回匹配所选条件的项目的函数。它们只能分开工作。查找符合所有条件的对象的最佳方法是什么?
我试图对所有可能的选项进行 if 语句,但代码无法正常工作,看起来应该有一些更好的选择。
下面是函数示例:
function chooseRating(hotel) {
return hotel.rating == e.target.value;
}
function chooseMeal(hotel) {
return hotel.mealType == e.target.value;
}
function choosePlace(hotel) {
for (let l = 0; l < chosenPlace.length; l++) {
if(chosenPlace[l].checked) {
return hotel.region == e.target.value;
}
}
}Run Code Online (Sandbox Code Playgroud)
我应该如何用它过滤数组?
let filteredCards = hotels.filter(function(hotel, index, hotels) {
// ??
});Run Code Online (Sandbox Code Playgroud)
用户选择他对酒店的要求,他应该得到符合所有要求的酒店。如果其中一些未选择,则默认情况下不计算在内。
javascript ×1