【发布时间】:2019-10-15 17:23:41
【问题描述】:
我的用例是这样的。
- 我有一个包含对象的数组。
- 每个对象都有一个名为
menu的数组 - 该菜单数组再次遭到反对。
- 每个对象都有一个数组
dish_has_categories - 在
dish_has_categories数组中,如果有一个CategoryId等于8的对象我想过滤掉那个根对象。
我的原始数据对象
const data = [{
menuName: "Hot dogs",
menu: [
{
dishId: '1',
dish_has_categories: [{
CategoryId: '8'
}]
},
{
dishId: '2',
dish_has_categories: [{
CategoryId: '9'
}]
}]
},
{
menuName: "Burgers",
menu: [{
dishId: '3',
dish_has_categories: [{
CategoryId: '6'
}]
}, {
dishId: '4',
dish_has_categories: [{
CategoryId: '4'
}]
}]
},
{
name: "Drinks",
menu: []
}
]
我的预期结果是
[{
menuName: "Hot dogs",
menu: [
{
dishId: '1',
dish_has_categories: [{
CategoryId: '8'
}]
},
{
dishId: '2',
dish_has_categories: [{
CategoryId: '9'
}]
}]
}]
到目前为止我所做的是
const data2 = data.filter(element => {
return element.menu.length > 0
})
我不知道如何在嵌套对象和数组中进行深度过滤。希望大家都清楚我的问题。
【问题讨论】:
标签: javascript arrays javascript-objects