I've an json data that has an array. Each element of an array has array named items. I want to remove the inner array element if the string doesnot match the itemName. I tried using filter method but its not working as expected. It only returns the matching inner array elements but all the outer array elements are removed too. P.S see the code, json data, the output I get and the expected output. Thanks in advance
Code:
const update2 =
state.menuResponse.map(
item => {
return item.items.filter(item => (item.itemName).includes("C"))
})
console.log("menuSearcheddd", update2)
Json data
{
"data": [
{
"menuHeader": "Starters",
"items": [
{
"itemId": "1",
"itemName": "burger",
},
{
"itemId": "ITD1ZYTH",
"itemName": "Cheese balls",
}
]
},
{
"menuHeader": "string",
"items": [
{
"itemId": "1",
"itemName": "burger",
},
{
"itemId": "2",
"itemName": "burger-non veg",
}
]
}
]
}
Output:
{
"data": [
{
"items": [
{
"itemId": "ITD1ZYTH",
"itemName": "Cheese balls",
}
]
},
{
"items": []
}
]
}
What should be?
{
"data": [
{
"menuHeader": "Starters",
"items": [
{
"itemId": "ITD1ZYTH",
"itemName": "Cheese balls",
}
]
},
{
"menuHeader": "string",
"items": []
}
]
}