SOURCE

var result = []
function aa(temp) {
    var sum = []
    for (let i = 0; i < temp.length; i++) {
        var a = {}
        a["menuId"] = temp[i]["menuPid"]
        var b = []
        var c = {}
        c["menuId"] = temp[i]["menuId"]
        b.push(c)
        a["children"] = b
        //console.log(a)
        sum.push(a)
    }
    for (let i = 0; i < sum.length; i++) {
        for (let j = i + 1; j < sum.length; j++) {
            if (sum[i]["menuId"].length > sum[j]["menuId"]) {
                let a = sum[i]
                sum[i] = sum[j]
                sum[j] = a
            }
        }
    }
    console.log(sum)
    //result.push(sum[0])
    function repeat(a, result) {
        for (let j = 0; j < result.length; j++) {
            if (a["menuId"].indexOf(result[j]["menuId"]) != -1) {
                console.log(result[j]["menuId"])
                if (a["menuId"] == result[j]["menuId"]) {
                    if (result[j]["children"] == undefined) {
                        result[j]["children"] = []
                        result[j]["children"].push(a["children"])
                        return
                    } else {
                        result[j]["children"].push(a["children"])
                        return
                    }
                }
                console.log("ok")
                if (result[j]["children"] === undefined) {
                    result[j]["children"] = []
                    result[j]["children"].push(a)
                    return;
                } else {
                    console.log("www"+j)
                    repeat(a, result[j]["children"])
                }
            } 
        }
        result.push(a)
        return true
    }
    repeat(sum[0], result)
    repeat(sum[1], result)
    repeat(sum[2], result)
    //console.log(sum)
    return result
}

var a = [{ "menuId": "menu11", "menuPid": "menu1" }, { "menuId": "menu12", "menuPid": "menu1" }, { "menuId": "menu121", "menuPid": "menu12" }, { "menuId": "menu21", "menuPid": "menu2" }]
console.log(aa(a))
console.log("ss".indexOf("sss"))
console 命令行工具 X clear

                    
>
console