所以我在这里有这个测试虚拟数据:
{
_id: "1",
name: "Lakawon Island Resort",
price_per_night: 2804.0,
description:
"On a lush 16-hectare island with white-sand beaches, this relaxed resort is 5 km from the jetty in Cadiz Viejo, a village on the mainland.",
address: "Cadiz Viejo",
city: "New York",
state: "New York",
zip_code: "6121",
latitude: 11.045411,
longitude: 123.201465,
phone: "(034) 213 6354",
email: "info@example.com",
website: 'www.lakawonislandresort.us',
amenities:
{
tv: false,
reservation: false,
cabin: true,
services: true,
},
image:
"https://images.unsplash.com/photo-1512356181113-853a150f1aa7",
rating: 4.5,
reviews: 11,
},
请注意,我有这个对象amenities,它也是一个对象。目前我有 4 个子对象tv,reservation和cabin。services
我需要的是检查每个项目是否为真,如果它们确实为真,则返回对象的键并在其旁边添加一个超棒的字体图标。否则,如果该值为 false,则不执行任何操作或返回 null。
到目前为止,这是我的实现方式:
{
amenities && Object.entries(amenities).map(
([key, value]) => {
if(key === 'tv' && key[value] === true){
return `<p><i class="fa fa-tv"></i> ${key}</p>`
} else if (key === 'reservation' && key[value] === true){
return `<p><i class="fa fa-reservation"></i> ${key}</p>`
}else if (key === 'cabin' && key[value] === true){
return `<p><i class="fa fa-cabin"></i> ${key}</p>`
}else if (key === 'services' && key[value] === true){
return `<p><i class="fa fa-service"></i> ${key}</p>`
}
return null
}
)
}
但是,这不起作用,而是返回错误Can't map through object。
知道如何检查每个键是否为真,然后如果值为真,则在其旁边输出各自的 fontawesome 图标?有没有比 if else 语句更有效的方法来做到这一点?
达令说
慕后森
元芳怎么了
相关分类