Js json tree object from the child to find all the outer parent ID how to find?

let arrn=[
        {
            "MENU_URL": "dashboard",
            "MENU_ID": "104a580029c54e139210b7e87dca6d82",
            "MENU_NAME": ""
        },
        {
            "MENU_URL": "systemManage",
            "MENU_ID": "769130d1918d47219e6f0c463a1c9c67",
            "MENU_NAME": ""
        },
        {
            "MENU_URL": "serviceManage",
            "MENU_ID": "8619ca0b6fbc42649a27475e339d5d4a",
            "MENU_INFO": [
                {
                    "MENU_URL": "userInfo",
                    "MENU_ID": "104a580029c54e139210b7e87dca6d87",
                    "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": ""
        }
    ]

jsonID
Mar.29,2021

answered a similar question last year, stamped this , you just need to change the property in the method to the property you need. That method returns the completion path array, that is, [parent, child]. You only need the parent element array to remove the last element.

send a string implementation for this problem, and the return result is an array [parent, master,.]

function findTopParents(menuJson, childId, result) {
    result = result || [];
    let menuStr = typeof menuJson === "string" ? menuJson : JSON.stringify(menuJson);
    let reg = new RegExp('MENU_ID":"([^"]+)"[^\\}\\]\\[\\{]+\\[\\{[^\\}\\]\\[\\{]+MENU_ID":"' + childId);

    if(reg.test(menuStr)) {
        result.push(menuStr.match(reg)[1]);
           return findTopParents(menuStr, menuStr.match(reg)[1], result);
   } else {
           return result;
   }
}

var ret = findTopParents(arrn, '454a580029c54e139210b7e87dca6d82');
console.log(ret); // ["104a580029c54e139210b7e87dca6d80", "d379e3f38cb3472ab18287034f32ef46"]

 function generateID(arr){
    if(Array.isArray(arr)){
        return arr.map(item => {
            let temp = [];
            temp.push(item.MENU_ID);
            if(item.MENU_INFO){
                temp.push(generateID(item.MENU_INFO));
            }
                return temp;
        });
    }
}

let result = generateID(arrn);

result.map(item => {
    return item.join(',').split(',');
})

:
[Array(1), Array(1), Array(2), Array(4)]
0: ["104a580029c54e139210b7e87dca6d82"]
1: ["769130d1918d47219e6f0c463a1c9c67"]
2: (2)["8619ca0b6fbc42649a27475e339d5d4a", "104a580029c54e139210b7e87dca6d87"]
3: (4)["d379e3f38cb3472ab18287034f32ef46", "104a580029c54e139210b7e87dca6d80", "454a580029c54e139210b7e87dca6d82", "104a580029c54e139210b7e87dca6d89"]
length: 4
__proto__: Array(0)

if you don't need the last subitem, you can delete it, then drop the empty array Filter, or change the judgment condition

. The objects in

JS do not have the ability to trace up the source, so you have to build your own data structure.


if this is the format, we have no choice but to traverse. You can build both levels and parentId as a property that is convenient to use later

Menu