I am currently working on integrating search functionality with a multilevel array.
Let's suppose I have the following JSON multilevel array:
const data = [
{
menu: 'Example',
submenu: [
{
menu: 'Example1',
},
{
menu: 'Example2',
submenu: [
{
menu: 'Example2.1',
},
{
menu: 'Example2.2',
},
{
menu: 'Example2.3',
},
],
},
{
menu: 'NewTest',
submenu: [
{
menu: 'NewTest1.1',
},
{
menu: 'NewTest1.2'
},
{
menu: 'NewTest1.3',
},
],
},
],
}]
If I enter "Example2.3" in the search box, I would like the JSON to be formatted as follows:
const data = [
{
menu: 'Example',
submenu: [
{
menu: 'Example2',
submenu: [
{
menu: 'Example2.3'
},
],
},
],
}]
I came across an article at Using array.filter down multiple levels but it did not provide the desired output. It returned all the submenu items instead.
const data = [
{
menu: 'Example',
submenu: [
{
menu: 'Example1',
},
{
menu: 'Example2',
submenu: [
{
menu: 'Example2.1',
},
{
menu: 'Example2.2',
},
{
menu: 'Example2.3',
},
],
},
],
}]
Is there another approach that can help me achieve my desired result? Thank you in advance