I have an array of objects that contains all my routes. I need to access all object's nested children properties & their properties and combining the together.
My array of object looks similar to this:
const routes = [
{
path: '/system-settings1',
name: 'global-settings1',
component: 'tabs1',
children: [
{
path: 'level2-accounting1',
name: 'local-settings1',
components: 'modals1',
children: [
{
path: 'level3-accounting1',
name: 'local-settings1',
components: 'modals1'
}
// more children deeply nested(or not)
]
},
{
path: 'level2-accounting1',
name: 'local-settings1',
components: 'modals1',
children: [
{
path: 'level3-accounting1',
name: 'local-settings1',
components: 'modals1'
}
// more children deeply nested(or not)
]
}
],
},
{
path: '/system-settings2',
name: 'global-settings2',
component: 'tabs2',
children: [
{
path: 'level2-accounting2',
name: 'local-settings2',
components: 'modals2',
children: [
{
path: 'level3-accounting2',
name: 'local-settings2',
components: 'modals2'
}
// more children deeply nested(or not)
]
},
{
path: 'level3-accounting2',
name: 'local-settings2',
components: 'modals2',
children: [
{
path: 'level4-accounting2',
name: 'local-settings2',
components: 'modals2'
}
// more children deeply nested(or not)
],
}
],
},
// more objects with similar key/value pairs
];
I need to make the array of objects into single level array flat like so:
[
{
path: '/system-settings1',
name: 'global-settings1',
component: 'tabs1',
},
{
path: 'level2-accounting2',
name: 'local-settings2',
components: 'modals2',
},
{
path: 'level3-accounting1',
name: 'local-settings1',
components: 'modals1'
},
{
path: 'level2-accounting1',
name: 'local-settings1',
components: 'modals1',
}
// so on if there is more objects etc
]
I have tried to .map()
& .filter()
combine with a while
loop, but to be honest I'm lacking the skills to accomplish by my own & it's not worth to include my attempts. If anybody could assist me with this & explain it would be extremely appreciated.