Parsing nested data, how to write recursion

something like this

const demo = [{
        path: "/login",
        name: "login",
        meta: {
            title: "Login - ",
            hideInMenu: true
        }
    },
    {
        path: "/",
        name: "_home",
        redirect: "/home",
        meta: {
            hideInMenu: true,
            notCache: true
        },
        children: [{
            path: "/home",
            name: "home",
            meta: {
                hideInMenu: true,
                title: "",
                notCache: true,
                icon: "md-home"
            }
        }]
    },
    {
        path: "/system",
        name: "system",
        meta: {
            icon: "md-menu",
            title: ""
        },
        children: [{
                path: "user",
                name: "user",
                meta: {
                    icon: "ios-people-outline",
                    title: ""
                }
            },
            {
                path: "menus",
                name: "menus",
                meta: {
                    icon: "md-funnel",
                    title: ""
                }
            },
            {
                path: "character",
                name: "character",
                meta: {
                    icon: "md-funnel",
                    title: ""
                }
            }
        ]
    },
    {
        path: "/401",
        name: "error_401",
        meta: {
            hideInMenu: true
        }
    },
    {
        path: "/500",
        name: "error_500",
        meta: {
            hideInMenu: true
        }
    },
    {
        path: "*",
        name: "error_404",
        meta: {
            hideInMenu: true
        }
    }
]

convert to this

const demo1=[
    {
        title:"",
        children:[
            {
                title:""
            },
            {
                title:""
            },
            {
                title:""
            }
        ]
    }
]

it can be realized with for that you don"t know how to write it with recursion

Nov.23,2021

try this:


function convertMenueTree(arr) {
    let resultArr = [];   
    arr.map((item) => {
        if(item.children) { //  
            resultArr.push({title:item.meta.title,children:convertMenueTree(item.children)})
        } else { // 
            resultArr.push({title:item.meta.title})
        }
    })    
    return resultArr;
}

convertMenueTree(demo)

the above assumption is that you have meta: {title:*} on each layer.
if not, you can use

 else {
         if(meta){....meta}
        resultArr.push({title:item.meta.title})
    }
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3be63-2c2d9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3be63-2c2d9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?