我有多家酒店:
var hotels = [
{
name: 'Ritz Carlton',
rating: 5,
description: 'The most luxurious place in the world',
mealType: 'AI',
features: ['wifi', 'conference', 'beach'],
region: 'Kemer'
},
etc.
我正在创建一个过滤器函数,该函数将返回经过过滤的,符合某些条件的酒店数组。
为什么此代码起作用?
if(formRating.value == 5) {return hotel.rating == 5;}
并且此功能不:
function chooseFeaures(hotel) {
Array.from(chosenFeature).forEach(feature => {
if(feature.checked) {
if(feature.value == 'wifi') {return hotel.features.includes('wifi');}
if(feature.value == 'beach') {return hotel.features.includes('beach');}
if(feature.value == 'gym') {return hotel.features.includes('gym');}
if(feature.value == 'conference') {return hotel.features.includes('conference');}
else {return hotel.features == 'wifi';}
}
});
}
当我做if(feature.checked){console.log(feature.value)};它有效
if(feature.value =='beach'){console.log('beach');}也可以
当我这样做时:
let filtered = hotels.filter( hotel => {
return hotel.features.includes('gym');
});
它也可以工作。
输入看起来像这样:
<label>
<input type="checkbox" class="filled-in" value="wifi" name="feature"/>
<span>Wi-Fi</span>
</label>
变量:让selectedFeature = document.getElementsByName('feature');
并且函数正在内部运行:
let filteredCards = hotels.filter(function(hotel, index, hotels) {
return chooseFeaures(hotel);
});
很抱歉这么长的问题,但是我想使其尽可能详细