i've got the following array of objects
let prova: ActiveRoute[] = [
{
path: '/Root',
method: 'GET',
children: [
{
path: '/Son',
method: 'GET',
children: [
{
path: '/Grandson',
method: 'GET',
children: [
{
path: '/Boh',
method: 'GET',
activeMessage: 'End',
}
],
}
],
}
],
middleware: [
'middleware1',
],
}
This is the ActiveRoute interface
export interface ActiveRoute {
path: string;
children?: ActiveRoute[];
middleware?: string[];
method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
activeMessage?: string;
}
I want to print all the path properties in a string. What should i do?
This is what i've done (wrong)
function getEndPoints(prova) {
let endpoints: string = '';
prova.forEach((r) => {
if (r.path) {
endpoints += r.path;
if(r.children){
r.children.forEach((s) => {
if (s.path) {
endpoints += s.path;
}
if (s.children){
s.children.forEach((z) =>{
if (z.path){
endpoints += z.path;
}
});
}
});
}
}
});
console.log(endpoints);
}
I really don't understand how should i loop continuously and deeply within an array of objects. This is my desire output, in this case: '/Root/Son/Grandson/Boh'.
Obviously now i don't how i'll go deep within.
/Root/Son/Grandson/BohWhat if you have multiple children on any of the levels?.