Js json object from the child to find the outermost layer how to find the parent, is there a way

the parent can find the direct point of the subset, but the subset can find the parent. How to operate

let arrn=[
        {
            "MENU_URL": "dashboard",
            "MENU_ID": "104a580029c54e139210b7e87dca6d89",
            "MENU_NAME": ""
        },
        {
            "MENU_URL": "systemManage",
            "MENU_ID": "769130d1918d47219e6f0c463a1c9c67",
            "MENU_NAME": ""
        },
        {
            "MENU_URL": "serviceManage",
            "MENU_ID": "8619ca0b6fbc42649a27475e339d5d4a",
            "MENU_INFO": [
                {
                    "MENU_URL": "userInfo",
                    "MENU_ID": "104a580029c54e139210b7e87dca6d89",
                    "MENU_NAME": ""
                }
            ],
            "MENU_NAME": ""
        },
        {
            "MENU_URL": "serviceManage/recordQuery",
            "MENU_ID": "d379e3f38cb3472ab18287034f32ef46",
            "MENU_INFO": [
                {
                    "MENU_URL": "dashboard",
                    "MENU_ID": "104a580029c54e139210b7e87dca6d80",
                    "MENU_INFO": [
                        {
                            "MENU_URL": "dashboard",
                            "MENU_ID": "454a580029c54e139210b7e87dca6d82",
                            "MENU_NAME": "222"
                        }
                    ],
                    "MENU_NAME": "111"
                },
                {
                    "MENU_URL": "dashboard",
                    "MENU_ID": "104a580029c54e139210b7e87dca6d89",
                    "MENU_NAME": "333"
                }
            ],
            "MENU_NAME": ""
        }
    ]
222MENU_IDMENU_ID



```
function getParentId(childId) {
    tmparr.forEach(function (item, index) {
        if (item.MENU_INFO) {
            item.MENU_INFO.forEach(function (list, idx) {
                if (list.MENU_ID === childId) {
                    console.log("d", item)
                }
            })
        }
    })
}
getParentId("104a580029c54e139210b7e87dca6d80")
```
Mar.28,2021

Recursive traversal

function findParentById(arr,id){
    var parentId = '',
    hasParentId = function loop(arr){
        return arr.some((item)=>{
            if(item.MENU_ID === id){
                return true
            }else if(Array.isArray(item.MENU_INFO)){
                parentId = item.MENU_ID
                return loop(item.MENU_INFO)
            }else{
                return false
            }
        })
    }(arr)
    return hasParentId ? parentId : ''
}

function findParentsById(arr,id){
    var parentIds = [],
        index = 0,
    hasParentId = function loop(arr, index){
        return arr.some((item)=>{
            if(item.MENU_ID === id){
                parentIds = parentIds.slice(0, index)
                return true
            }else if(Array.isArray(item.MENU_INFO)){
                parentIds[index] = item.MENU_ID
                return loop(item.MENU_INFO, index+1)
            }else{
                return false
            }
        })
    }(arr, index)
    return hasParentId ? parentIds : []
}

findParentById(arrn, '454a580029c54e139210b7e87dca6d82');//"104a580029c54e139210b7e87dca6d80"
findParentsById(arrn, '454a580029c54e139210b7e87dca6d82');
//["d379e3f38cb3472ab18287034f32ef46", "104a580029c54e139210b7e87dca6d80"]

here I use 104a580029c54e139210b7e87dca6d89 to test, and I find that I can't find it all the time. Finally, I see that your id is very repetitive, for example, the id of the first item of data is also this, so is this just your test data or is the data repeatable like this? If you can repeat the above code, you need to change it, but it's all recursive

.

there is a clever way: convert the array object arrn corresponding to Menu into a string, and then use regular lookup, which is extremely easy and can be adapted to infinitely deep levels.

function findTopParent(menuJson, childId) {
   let menuStr = JSON.stringify(menuJson);
   let reg = new RegExp('MENU_ID":"([^"]+)"[^\\}\\]]+MENU_ID":"' + childId);
   return reg.test(menuStr) ? menuStr.match(reg)[1]: undefined;
}

findTopParent(arrn, '104a580029c54e139210b7e87dca6d89');  //8619ca0b6fbc42649a27475e339d5d4a
findTopParent(arrn, '454a580029c54e139210b7e87dca6d82');  //d379e3f38cb3472ab18287034f32ef46

how did you get the subset? It is the parent that falls behind the subset.


I can find the parent id,. I may have described a problem. I am looking for the outermost parent id

.
Menu