I'm trying to filter an array of objects based off a property and also filter an array of objects within. Can I use filter within a filter to loop through and reduce the second array?
Something like the following:
const menu = [
{
name: "Batman",
showinMenu: true,
categories: [
{
title: "Title 2",
showinMenu: true,
}
,
{
title: "Title 3",
showinMenu: false,
}
]
},
{
name: "Superman",
showinMenu: true,
categories: [
{
title: "Test 2",
showinMenu: true,
}
,
{
title: "Test 3",
showinMenu: false,
}
]
},
]
const Filtered = menu?.categories?.filter((category) => {
category.categories?.filter((subcategory) => {
return subcategory.showinMenu === true
})
return category.showinMenu === true
})
Expected output
[
{
name: "Batman",
showinMenu: true,
categories: [
{
title: "Title 2",
showinMenu: true,
}
]
}
]