功能描述:点选标签,过滤显示列表内容。现将选中标签拼成一个数组,比如sId = ["1","2","3"]
,然后取出数据中 tag
数组中 id
等于 sId
数组中某一项的内容,取交集。
最后返回过滤后的 hotelList
1.选中标签数据:const sId = ["1","2","3"]
2.列表数据:
data: {
hotelList: [{
"name": "A级房",
"hotelRoomDetails": [{
"price": 2360,
"member": "0",
"tag": [
{
"id": "1",
"name": "95折"
},
{
"id": "2",
"name": "会员"
},
{
"id": "3",
"name": "88折"
}
]
}, {
"price": 2560,
"member": "1",
"tag": [
{
"id": "1",
"name": "含早"
}]
}]
},
{
"name": "B房",
"des": "58m² | 双床",
"hotelRoomDetails": [{
"price": 3390,
"member": "1",
"tag": [
{
"id": "1",
"name": "含早"
}]
}, {
"price": 3680,
"member": "0",
"tag": [
{
"id": "1",
"name": "含早"
},
{
"id": "2",
"name": "会员价"
}
]
}]
},
{
"name": "总统套房",
"hotelRoomDetails": [{
"price": 8990,
"member": "0",
"tag": null
}]
}
]
}
3.当前错误写法(map和filter混合使用):
const filteredResults = this.data.hotelList.map(itemParent => itemParent.filter(itemChild => {
return slectedPreId.includes(itemChild.hotelRoomDetails.preferential)
}))
ps:如果数据多,这个复杂度稍微有点高,可将
slectedPreId
先转为Set
去查询是否包含