Implement tree structure data es6

var jsonArray = {

"data":{
    "List": [{
                "authorityId": 1,
                "authorityName": "",
                "grade": "1001",
                "fatherId": "0"
            },
            {
                "authorityId": 2,
                "authorityName": "",
                "grade": "1002",
                "fatherId": "0"
            },
            {
                "authorityId": 3,
                "authorityName": "",
                "grade": "1003",
                "fatherId": "0"
            },
            {
                "authorityId": 4,
                "authorityName": "",
                "grade": "1004",
                "fatherId": "0"
            },
            {
                "authorityId": 5,
                "authorityName": "",
                "grade": "1005",
                "fatherId": "0"
            },
            {
                "authorityId": 6,
                "authorityName": "",
                "grade": "2101",
                "fatherId": "1001"
            },
            {
                "authorityId": 7,
                "authorityName": "",
                "grade": "2102",
                "fatherId": "1001"
            },
            {
                "authorityId": 8,
                "authorityName": "",
                "grade": "2103",
                "fatherId": "1001",
            },
            {
                "authorityId": 9,
                "authorityName": "",
                "grade": "2201",
                "fatherId": "1002"
            },
            {
                "authorityId": 10,
                "authorityName": "",
                "grade": "2202",
                "fatherId": "1002"
            },
            {
                "authorityId": 11,
                "authorityName": "",
                "grade": "2301",
                "fatherId": "1003"
            },
            {
                "authorityId": 12,
                "authorityName": "",
                "grade": "2302",
                "fatherId": "1003"
            }
        ]
}

}
expect to spell the following tree data format (implemented in jq or es6)
correspondence: fatherid
data: of the parent"s grade= child [

{
      label: " 1",
      children: [{
        label: " 1-1",
        children: [{
          label: " 1-1-1"
        }]
      }]
    }, {
      label: " 2",
      children: [{
        label: " 2-1",
        children: [{
          label: " 2-1-1"
        }]
      }, {
        label: " 2-2",
        children: [{
          label: " 2-2-1"
        }]
      }]
    }

]

May.07,2021

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
            var list = [{
                "authorityId": 1,
                "authorityName": "",
                "grade": "1001",
                "fatherId": "0"
            },
            {
                "authorityId": 2,
                "authorityName": "",
                "grade": "1002",
                "fatherId": "0"
            },
            {
                "authorityId": 3,
                "authorityName": "",
                "grade": "1003",
                "fatherId": "0"
            },
            {
                "authorityId": 4,
                "authorityName": "",
                "grade": "1004",
                "fatherId": "0"
            },
            {
                "authorityId": 5,
                "authorityName": "",
                "grade": "1005",
                "fatherId": "0"
            },
            {
                "authorityId": 6,
                "authorityName": "",
                "grade": "2101",
                "fatherId": "1001"
            },
            {
                "authorityId": 7,
                "authorityName": "",
                "grade": "2102",
                "fatherId": "1001"
            },
            {
                "authorityId": 8,
                "authorityName": "",
                "grade": "2103",
                "fatherId": "1001",
            },
            {
                "authorityId": 9,
                "authorityName": "",
                "grade": "2201",
                "fatherId": "1002"
            },
            {
                "authorityId": 10,
                "authorityName": "",
                "grade": "2202",
                "fatherId": "1002"
            },
            {
                "authorityId": 11,
                "authorityName": "",
                "grade": "2301",
                "fatherId": "1003"
            },
            {
                "authorityId": 12,
                "authorityName": "",
                "grade": "2302",
                "fatherId": "1003"
            }
        ]

        var data = []


        function loop(list, data, fatherId) {
            list.forEach(item => {
                if (item.fatherId === fatherId) {
                    var child = {
                        label: item.authorityName,
                        grade: item.grade,
                        children: []
                    }

                    loop(list, child.children, item.grade)

                    data.push(child)
                }
            })
        }
        
        loop(list, data, '0')

        console.log(data)

    </script>
</body>
</html>

function formatDataTree(data,id,pid) {
    let _data = JSON.parse(JSON.stringify(data));
    return _data.filter( p => {
        const _arr = _data.filter( c => p[id] === c[pid] );
        _arr.length && (p.children = _arr)
        return p[pid] === 0
    })
};

let tree = formatDataTree(data,"grade","fatherId");

console.log(tree);
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-1b2c33d-2bb24.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-1b2c33d-2bb24.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?