struggling to figure this out. What is the cleanest code to extract all paths from a multi-dimentional array?
Below is my data.
const Pages = [
{
path: ['home'],
},
{
path: [''],
subpages: [
{
path: ['about', 'congleton'],
subpages: [
{
path: [ 'about', 'congleton', 'opening' ]
}
]
}
]
}
]
And this is the expected outcome:
[ ['home'], [''], ['about', 'congleton'], [ 'about', 'congleton', 'opening' ] ]
This is what I have right now, but this doesn’t work:
const getPath = function (page) {
let paths = page.path;
if (page.subpages) {
const sub = page.subpages ? page.subpages.map((x) => getPath(x)) : [];
paths = paths.concat(sub);
}
return paths;
};
const paths = Pages.map((x) => getPath(x));
console.log(paths);