Js object array recursion

Multi-level nested array of objects, there is an id, to find according to this id, and this id is related to the id, I write as follows, I do not know where the problem?

clipboard.png

as follows, the id is 113. You want to get an array like 1Magne11113 (not implemented through str.splice, etc.)
var arr = [

    {
        "menuId": 1,
        "menuName": "",
        "defaultIcon": "icon-wulianwangjiankong",

        "path": "/internet_things",
        "childs": [
            {
                "menuId": 11,
                "menuName": "",
                "defaultIcon": "icon-home",
                "path": "/internet_things/home",
                "childs": [
                    {
                        "menuId": 111,
                        "menuName": "",
                        "defaultIcon": "",
                        "path": "/internet_things/home/data_analyse",
                        "childs": []
                    },
                    {
                        "menuId": 112,
                        "menuName": "",
                        "defaultIcon": "",
                        "path": "/internet_things/home/device_loc",
                        "childs": []
                    }
              ]
             }
            ]
           }
          ];
Mar.23,2021

you can't express clearly in this way. If you're just looking for something related to id, you can intercept it with loops and strings.


id equals the parent id into the array


function testArr(arr, id){
    var resulte = []
    var loop = function(arr){
        return arr.some(item=>{
            if(item.menuId == id){
                resulte.unshift(item.menuId)
                return true
            }
            if(Array.isArray(item.childs)){
                var childHasId = loop(item.childs)
                childHasId && resulte.unshift(item.menuId)
                return childHasId
            }
        })
    }
    loop(arr)
    return resulte
}
testArr(arr,'112'); //[1, 11, 112]
.
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-1e46e06-44beb.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-1e46e06-44beb.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?