请问js如何递归三级菜单的数据?

{
        "364": {
            "data": {
                "id": 364,
                "pid": 0,
                "level": 0,
                "path": "-0-",
                "type": 1,
                "title": "工程技术",
                "description": "",
                "icon": "",
                "sort": 0,
                "add_time": "0000-00-00 00:00:00",
                "update_time": "2022-02-17 17:38:41",
                "is_hid": 0,
                "is_del": "0"
            },
            "child": {
                "378": {
                    "data": {
                        "id": 378,
                        "pid": 364,
                        "level": 1,
                        "path": "-0-364-",
                        "type": 1,
                        "title": "关键设备/部件",
                        "description": "",
                        "icon": "",
                        "sort": 0,
                        "add_time": "0000-00-00 00:00:00",
                        "update_time": "2022-02-17 17:38:52",
                        "is_hid": 0,
                        "is_del": "0"
                    },
                    "child": {
                        "383": {
                            "data": {
                                "id": 383,
                                "pid": 378,
                                "level": 2,
                                "path": "-0-364-378-",
                                "type": 1,
                                "title": "反应釜",
                                "description": "",
                                "icon": "",
                                "sort": 0,
                                "add_time": "0000-00-00 00:00:00",
                                "update_time": "2022-03-23 11:15:35",
                                "is_hid": 0,
                                "is_del": "0"
                            },
                            "child": []
                        }
                    }
                },
                
            }
        },
        "365": {
            "data": {
                "id": 365,
                "pid": 0,
                "level": 0,
                "path": "-0-",
                "type": 1,
                "title": "维护保养",
                "description": "",
                "icon": "",
                "sort": 0,
                "add_time": "0000-00-00 00:00:00",
                "update_time": "0000-00-00 00:00:00",
                "is_hid": 0,
                "is_del": "0"
            },
            "child": {
                "376": {
                    "data": {
                        "id": 376,
                        "pid": 365,
                        "level": 1,
                        "path": "-0-365-",
                        "type": 1,
                        "title": "动设备",
                        "description": "",
                        "icon": "",
                        "sort": 0,
                        "add_time": "0000-00-00 00:00:00",
                        "update_time": "0000-00-00 00:00:00",
                        "is_hid": 0,
                        "is_del": "0"
                    },
                    "child": []
                },
                "377": {
                    "data": {
                        "id": 377,
                        "pid": 365,
                        "level": 1,
                        "path": "-0-365-",
                        "type": 1,
                        "title": "静设备",
                        "description": "",
                        "icon": "",
                        "sort": 0,
                        "add_time": "0000-00-00 00:00:00",
                        "update_time": "0000-00-00 00:00:00",
                        "is_hid": 0,
                        "is_del": "0"
                    },
                    "child": []
                }
            }
        }
    }


怎么通过js递归实现跟element-UI这种数据格式?

阅读 2.2k
3 个回答

能给出这样源数据结构的真是位神人,楼主请跳起来替我踹Y一脚,以示敬仰!

这样?

const data = {...};

function format(nodes) {
    return Object.values(nodes).map(({data: d, child: c}) => 
        ({value: d.id, label: d.title, ...(c && Object.keys(c).length ? {children: format(c)} : {})})
    )
}

const result = format(data);

结果:

[
  {
    "value": 364,
    "label": "工程技术",
    "children": [
      {
        "value": 378,
        "label": "关键设备/部件",
        "children": [
          {
            "value": 383,
            "label": "反应釜"
          }
        ]
      }
    ]
  },
  {
    "value": 365,
    "label": "维护保养",
    "children": [
      {
        "value": 376,
        "label": "动设备"
      },
      {
        "value": 377,
        "label": "静设备"
      }
    ]
  }
]
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题