admin管理员组文章数量:1192330
My use case is something like this.
- I have an array that has an object.
- That each object has an array called
menu
- Again that menu array has objected.
- That each object has an array
dish_has_categories
- In
dish_has_categories
array, if there is an object with CategoryId is equal to8
I want to filter out that root object.
My original data object
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: []
}
]
My use case is something like this.
- I have an array that has an object.
- That each object has an array called
menu
- Again that menu array has objected.
- That each object has an array
dish_has_categories
- In
dish_has_categories
array, if there is an object with CategoryId is equal to8
I want to filter out that root object.
My original data object
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: []
}
]
My expect result is
[{
menuName: "Hot dogs",
menu: [
{
dishId: '1',
dish_has_categories: [{
CategoryId: '8'
}]
},
{
dishId: '2',
dish_has_categories: [{
CategoryId: '9'
}]
}]
}]
what I've done up to now is
const data2 = data.filter(element => {
return element.menu.length > 0
})
I have no idea how to deep filter inside nested objects and arrays. Hope my question is clear to you all.
Share Improve this question edited Aug 31, 2022 at 15:44 isherwood 61k16 gold badges120 silver badges168 bronze badges asked May 30, 2019 at 4:40 margherita pizzamargherita pizza 7,14529 gold badges102 silver badges178 bronze badges 1- Does this answer your question? Filtering array of objects by searching nested object properties – Heretic Monkey Commented Feb 12, 2021 at 17:01
3 Answers
Reset to default 18You can use filter()
with nested some()
.
The
some()
method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value
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: [] } ]
const res = data.filter(x =>
x.menu.some(y =>
y.dish_has_categories.some(z => z.CategoryId === '8')
)
);
console.log(res)
You can use filter and some
Here nested some is used to check whether any of dish_has_categories
has CategoryId
equal to '8'
, if it is true
then we include that menu in final output else we don't
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: [] } ]
let op = data.filter(val => {
let menu = val.menu.some(({dish_has_categories}) => dish_has_categories.some(({CategoryId}) => CategoryId === '8'))
return menu
})
console.log('filtered values -->\n',op)
let names = op.map(({menuName})=> menuName)
console.log('Names --> \n', names)
You can use javascript filter()
and some()
. some()
method checks if any of the elements in an array pass the function.
let data2 = data.filter(element => {
let menu = element.menu.some(({dish_has_categories}) => dish_has_categories.some(({CategoryId}) => CategoryId === '8'));
return menu;
});
console.log(data2);
本文标签: How can I filter nested objects and arrays with JavaScriptStack Overflow
版权声明:本文标题:How can I filter nested objects and arrays with JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738425864a2086119.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论