根据列表中字段的父子级关系转为树结构

将如下结构转成父节点为type,子节点为project,孙子节点为byLaws

[
    {
        "lineCode": "tj10",
        "lineName": "天津10号线",
        "type": "培训管理",
        "project": "无",
        "byLaws": "培训期间,违反培训管理相关规定的。",
        "assessCriteria": -200,
        "deductionCriteria": 0
    },
    {
        "lineCode": "tj10",
        "lineName": "天津10号线",
        "type": "任务管理",
        "project": "2级",
        "byLaws": "未按计划参加安全会或月度培训的。",
        "assessCriteria": -1500,
        "deductionCriteria": 0
    },
    {
        "lineCode": "tj10",
        "lineName": "天津10号线",
        "type": "任务管理",
        "project": "2级",
        "byLaws": "测试度培训的。",
        "assessCriteria": -1500,
        "deductionCriteria": 0
    },
    {
        "lineCode": "tj10",
        "lineName": "天津10号线",
        "type": "培训管理",
        "project": "中国",
        "byLaws": "培训期间,违反培训管理相关规定的。",
        "assessCriteria": -200,
        "deductionCriteria": 0
    }
]
function buildTree(data) {
  let nodeId = 1;
  const list = [];
  const obj = {};
  data.forEach(item => {
    const type = item.type;
    const project = item.project;
    const byLaws = item.byLaws
    if (!obj[type]) {
      obj[type] = { id: nodeId++, label: type, children: [] };
      list.push(obj[type]);
    }
    const projects = obj[type].children;
    let projectNode = projects.find(p => p.label === project)
    if (!projectNode) {
      projectNode = { id: nodeId++, label: project, children: [] };
      projects.push(projectNode);
    }
    projectNode.children.push({ id: nodeId++, label: byLaws });
  });
  return list;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值