In a recursive array, a field below that level is returned according to the characteristic ID,

problem description

data is a recursive tree
you need to find out the name under the url according to the unique key url

the environmental background of the problems and what methods you have tried

related codes

/ / Please paste the code text below (do not replace the code with pictures)


[{
    "menuUrl":1,
    "name":"666",
    "menuList":[{
        "menuUrl":3,
        "name":"hhaha",
        "menuList":[{
            "menuUrl":8,
            "name":"jajja",
            "menuList":[]
            }]
    }]

},{
    "menuUrl":88,
    "name":"ksls",
    "menuList":[{
        "menuUrl":55,
        "name":6,
        "menuList":[]
    }]

}]


findNameByUrl (menuUrl,data) = = > return name

for example, I, findNameByUrl (8heroin data) expect to get jajja

Nov.11,2021

not very elegant, main part:

let result;

function findNameByUrl(arr, url) {
    //
    arr.forEach(element => {
        if (element.menuUrl == url) {
            //
            result = element.name;
        }else if (element.menuList) {
            //
            findNameByUrl(element.menuList, url);
        }
    });
}

complete testable code:

let arr = [{
    "menuUrl": 1,
    "name": "666",
    "menuList": [{
        "menuUrl": 3,
        "name": "hhaha",
        "menuList": [{
            "menuUrl": 8,
            "name": "jajja",
            "menuList": []
        }]
    }]

}, {
    "menuUrl": 88,
    "name": "ksls",
    "menuList": [{
        "menuUrl": 55,
        "name": 6,
        "menuList": []
    }]

}];

let result;

function findNameByUrl(arr, url) {
    //
    arr.forEach(element => {
        if (element.menuUrl == url) {
            //
            result = element.name;
        }else if (element.menuList) {
            //
            findNameByUrl(element.menuList, url);
        }
    });
}

findNameByUrl(arr, 8);
console.log(result);

let arr = [{
    "menuUrl": 1,
    "name": "666",
    "menuList": [{
        "menuUrl": 3,
        "name": "hhaha",
        "menuList": [{
            "menuUrl": 8,
            "name": "jajja",
            "menuList": []
        }]
    }]

}, {
    "menuUrl": 88,
    "name": "ksls",
    "menuList": [{
        "menuUrl": 55,
        "name": 6,
        "menuList": []
    }]

}]

let crr = [];
let str = 8;

function Fn(arr, callback, crr) {
    arr.forEach(element => {
        for (let key in element) {
            if (key === "menuList") {
                Fn(element[key], callback, crr);
            } else {
                if (key === "menuUrl") {
                    if (callback(element[key])) {
                        crr.push(element[key]);
                    }
                }
            }
        }
    });
    return crr;

}

Fn(arr, function(data) {

    if (data === str) {
        return true;
    }
}, crr);

console.log(crr);

var data = [{
    "menuUrl": 1,
    "name": "666",
    "menuList": [{
        "menuUrl": 3,
        "name": "hhaha",
        "menuList": [{
            "menuUrl": 8,
            "name": "jajja",
            "menuList": []
        }]
    }]

}, {
    "menuUrl": 88,
    "name": "ksls",
    "menuList": [{
        "menuUrl": 55,
        "name": 6,
        "menuList": []
    }]

}];

function findNameByUrl(url, data) {
    var s = JSON.stringify(data), ss = s.split('menuUrl":' + url + ',"name":');
    if (ss.length == 2) {
        var x = ss[1], y = x.substr(0, x.indexOf(','));
        if (y[0] == '"') {
            y = y.substr(1, y.length - 2);
        }
        return y;
    }
}

console.log(findNameByUrl(8, data));
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-1b3a9fc-2c231.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-1b3a9fc-2c231.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?