Here is an array of objects that I am working with:
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',
],
}
Defined ActiveRoute interface:
export interface ActiveRoute {
path: string;
children?: ActiveRoute[];
middleware?: string[];
method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
activeMessage?: string;
I am attempting to extract and print all the 'path' properties in a single string.
This is what I have tried, but it is not giving me the desired output:
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 am struggling to figure out how to loop through an array of objects deeply and continuously. Desired output in this case: '/Root/Son/Grandson/Boh'.
Currently, I am unsure of how to navigate deeply within the array of objects.