【发布时间】:2020-10-23 13:11:54
【问题描述】:
我试图弄清楚如何根据用户的输入过滤一组对象。目前,我的搜索 btn 函数处理程序上有一个简单的 array.filter,见下文:
数组示例:
const items = [
{
id: Math.random().toFixed(5),
itemTitle: 'Pad Thai',
imageUrl:
'https://www.2foodtrippers.com/wp-content/uploads/2016/08/Delicious-Thai-Food-Pad-Thai.jpg.webp',
country: 'Thailand',
price: 5,
description: 'Street food in Bankok',
category: 'Food/Beverage',
},
{
id: Math.random().toFixed(5),
itemTitle: 'Canned coffee',
imageUrl:
'https://331mrnu3ylm2k3db3s1xd1hg-wpengine.netdna-ssl.com/wp-content/uploads/2015/08/Sprudge-VendingMachineCoffee-KateBeard-sprudgevending-3-740x493.jpg',
country: 'Japan',
price: 3.5,
description: 'Had to have one of this in Tokyo',
category: 'Food/Beverage',
},
{
id: Math.random().toFixed(5),
itemTitle: 'Scooter day rental',
imageUrl:
'https://anomadsdream.com/wp-content/uploads/2013/05/SAM_1859.jpg',
country: 'Vietnam',
price: 7.5,
description:
'Cheap rental bike, Ko Thao. This is average price around Thailand',
category: 'Transport',
},
{
id: Math.random().toFixed(5),
itemTitle: 'Bottle of Water Price',
imageUrl:
'https://2reb5l2d3joj3y7m8y2f5ptbefc-wpengine.netdna-ssl.com/order/files/2017/11/B1.jpg',
country: 'Thailand',
price: 5,
description: 'Street food Pad Thai',
category: 'Food/Beverage',
},
{
id: Math.random().toFixed(5),
itemTitle: 'Gold Ice Cream',
imageUrl:
'https://kaname-inn.com/wp/wp-content/uploads/2018/05/IMG_4388_11.jpg',
country: 'Japan',
price: 5,
description: 'Delicious golden ice cream in Kyoto',
category: 'Food/Beverage',
},
{
id: Math.random().toFixed(5),
itemTitle: 'Buddah bracelet',
imageUrl:
'https://c7.alamy.com/comp/M8XGMD/seoul-south-korea-march-6-2018-buddhist-bracelet-on-shop-at-jogyesa-M8XGMD.jpg',
country: 'Japan',
price: 5,
description: 'Bought this while street shopping in Seoul',
category: 'Food/Beverage',
},
{
id: Math.random().toFixed(5),
itemTitle: 'Suvarnabhumi Taxi',
imageUrl:
'https://static.bangkokpost.com/media/content/20160507/c1_962249_160507092902.jpg',
country: 'Thailand',
price: 5,
description: 'Taxi from the center Bankok',
category: 'Food/Beverage',
},
{
id: Math.random().toFixed(5),
itemTitle: 'Vietnamese Coffee',
imageUrl:
'https://thewoksoflife.com/wp-content/uploads/2017/12/vietnamese-coffee-7.jpg',
country: 'Vietnam',
price: 3.5,
description: 'Delicious! prices change but this is the average',
category: 'Food/Beverage',
},
];
函数处理程序
const searchBtnHandler = () => {
const itemElements = document.getElementById('cards-hook').children;
const countryValue = countryFilterAccess.value;
const searchCategory = searchCategoryMenu.value;
const filteredItems = items.filter((item) => {
return item.country === countryValue && item.category === searchCategory;
});
console.log(filteredItems);
};
国家价值和搜索类别是从页面中获取的。假设用户同时提供过滤器(国家和类别),我拥有的过滤器可以工作。但是,我还想让用户选择其中一个过滤器。我已经通过“if statements”尝试过这个,但数组返回空或“奇怪”的过滤器设置。
我想要的如下:
国家/地区为空 - 提供类别
给定国家 - 类别留空
有人可以帮忙吗?我看过其他回复,但他们认为过滤条件已经给出,这不是我的情况。
谢谢!
【问题讨论】:
-
“我已经通过“if statements”尝试过这个,但是数组返回空或“奇怪”的过滤器设置” - 请添加您的尝试,以便我们帮助您修复它.
-
使用
||而不是&&应该可以在您的过滤器中使用
标签: javascript