首页 新闻 会员 周边 捐助

js数据整理

0
悬赏园豆:15 [已解决问题] 解决于 2020-02-18 15:12

怎么将这样的数据:
[
{
"id": 1,
"key": "",
"name": "系统管理",
"level": 1,
"type": 1,
"icon": "",
"code": "A001",
"parentCode": "",
"num": "A-1",
},

        {
            "id": 7,
            "key": "",
            "name": "日志管理",
            "level": 1,
            "type": 1,
            "icon": "",
            "code": "A004",
            "parentCode": "",
            "num": "A-7",
           
        },
        {
            "id": 8,
            "key": "roleAdd",
            "name": "角色管理",
            "level": 2,
            "type": 1,
            "icon": "",
            "code": "B001",
            "parentCode": "A001",
            "num": "1-1",
            
        },
        {
            "id": 14,
            "key": "logoManage",
            "name": "日志管理",
            "level": 2,
            "type": 1,
            "icon": null,
            "code": "E001",
            "parentCode": "A004",
            "num": "1-1",
           
        }
    ]

变成这样的数据:
[
{
"id": 1,
"key": "",
"name": "系统管理",
"level": 1,
"type": 1,
"icon": "",
"code": "A001",
"parentCode": "",
"num": "A-1",
"chuildren":[{
"id": 8,
"key": "roleAdd",
"name": "角色管理",
"level": 2,
"type": 1,
"icon": "",
"code": "B001",
"parentCode": "A001",
"num": "1-1",
}]
},

        {
            "id": 7,
            "key": "",
            "name": "日志管理",
            "level": 1,
            "type": 1,
            "icon": "",
            "code": "A004",
            "parentCode": "",
            "num": "A-7",
             "children": [
             {
                "id": 14,
                "key": "logoManage",
                "name": "日志管理",
                "level": 2,
                "type": 1,
                "icon": null,
                "code": "E001",
                "parentCode": "A004",
                "num": "1-1",
                "emp1": null,
                "emp2": null,
                "emp3": null,
               "emp4": null
          }
      ]
    },
    ]
alger_li的主页 alger_li | 初学一级 | 园豆:40
提问于:2020-02-18 14:57
< >
分享
最佳答案
0
let list = res[0]["__menus__"];
      let temp = {};
      let tree = [];
        for(let i in list){
            temp[list[i].code] = list[i];
        }
        for(let i in temp){
            if(temp[i].parentCode) {
                if(!temp[temp[i].parentCode].children) {
                    temp[temp[i].parentCode].children = [];
                }
                temp[temp[i].parentCode].children.push(temp[i]);
            } else {
                tree.push(temp[i]);
            }
        }
alger_li | 初学一级 |园豆:40 | 2020-02-18 15:11
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册