An algorithm for expanding nodes in the location tree after page refresh

  1. directory tree initialization initially returns only a two-tier structure.
[
    {
      name: "",
      path: "/",
      id: 1,
      childs: 2,
      children: [
        {
           name: "01",
           path: "/01/",
           id: 2,
           childs: 2,
           children:null
        },
        {
           name: "02",
           path: "/02/",
           id: 3,
           childs: 1,
           children:null
        },
      ]
    }
]

when a node is clicked, it is expanded by whether the id and childs clicked are the child nodes under the current node 0 and add expanded: true,.
now the opening path is path: "all / first-level menu 01 / second-level menu 02 / third-level menu 04" how to match to all / first-level menu 01 / in the initialization directory tree through this path. Get children, through id to match all / first-level menu 01 / second-level menu 02 / get children, through id Match all / level 1 menus 01 / level 2 menus 02 / level 3 menus 04 in children, and expand it?

the idea is to use the path all / level 1 menus 01 / level 2 menus 02 / level 3 menus 04 to include Path in the first level matching tree.
Thank you.

Nov.11,2021

//
let arrOrigin = [{
    name: "",
    path: "/",
    id: 1,
    childs: 2,
    children: [
        {
            name: "01",
            path: "/01/",
            id: 2,
            childs: 2,
            children: null
        },
        {
            name: "02",
            path: "/02/",
            id: 3,
            childs: 1,
            children: null
        },
    ]
}];

//
let path = "/01/02/04";

let arrPath = path.split("/");
console.log(arrPath); //

//
function render(arrOrigin, arrPath) {
    if (arrPath.length > 0) {
    //

        //
        let currentNode = arrPath.shift();

        arrOrigin.forEach(element => {
            if (element.name == currentNode) {
                // name  expanded
                element.expanded = true;
                if (element.childs > 0 && element.children) {
                    //
                    return render(element.children, arrPath);
                }
            }
        });
    }
}


render(arrOrigin, arrPath);

//
console.log(JSON.stringify(arrOrigin, null, 4));

output:

[ '', '01', '02', '04' ]
[
    {
        "name": "",
        "path": "/",
        "id": 1,
        "childs": 2,
        "children": [
            {
                "name": "01",
                "path": "/01/",
                "id": 2,
                "childs": 2,
                "children": null,
                "expanded": true
            },
            {
                "name": "02",
                "path": "/02/",
                "id": 3,
                "childs": 1,
                "children": null
            }
        ],
        "expanded": true
    }
]
Menu