代码改变世界

根据ID获取该节点的所有父节点的对象

2022-01-10 00:30  龙恩0707  阅读(486)  评论(0编辑  收藏  举报
 var data2= [
      {
        "nodeType": 1,
        "nodeName": "NA61",
        "children": [
          { 
            "nodeType":2, 
            "nodeName":"A",
            "children": [
              { 
                "nodeType":3,
                "nodeName":"A2-1.NA61",
                "children":[
                  {
                    "nodeType":4,
                    "nodeName":"A",
                    "children": [
                      {"nodeType":5,"nodeName":"A10","children":null},
                      {"nodeType":5,"nodeName":"A11","children":null},
                      {"nodeType":5,"nodeName":"A01","children":null},
                      {"nodeType":5,"nodeName":"A12","children":null},
                      {"nodeType":5,"nodeName":"A02","children":null},
                      {"nodeType":5,"nodeName":"A13","children":null},
                      {"nodeType":5,"nodeName":"A03","children":null},
                      {"nodeType":5,"nodeName":"A14","children":null},
                      {"nodeType":5,"nodeName":"A04","children":null},
                      {"nodeType":5,"nodeName":"A15","children":null},
                      {"nodeType":5,"nodeName":"A05","children":null},
                      {"nodeType":5,"nodeName":"A16","children":null},
                      {"nodeType":5,"nodeName":"A06","children":null},
                      {"nodeType":5,"nodeName":"A07","children":null},
                      {"nodeType":5,"nodeName":"A08","children":null},
                      {"nodeType":5,"nodeName":"A09","children":null}
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ];
    // 如果开发 数据中没有返回一个唯一标识的话,自己和下面一样遍历下,自动添加 customId 当作数据中的唯一标识
    function renderTreeFunc(data) {
      let arrs = [];
      let i = 1;
      const loopFunc = function(data) {
        const rets = [];
        let obj = {};
        if (data.length > 0) {
          data.forEach(item => {
            const temp = { ...item };
            const { nodeName, nodeType } = item;
            obj = {
              'nodeName': nodeName,
              'nodeType': nodeType,
              'customId': i++,
            };
            if (temp.children && temp.children.length) {
              temp.children = loopFunc(temp.children);
              obj['children'] = temp.children;
            }
            rets.push(obj);
          })
        }
        return rets;
      }
      if (data && data.length) {
        arrs = loopFunc(data);
      }
      return arrs;
    }
    const rets = renderTreeFunc(data2);
    console.log('-----rets----', rets);
    /*
     * 上面返回宕数据如下:
     [
       {
         "nodeName": "NA61",
         "nodeType": 1,
         "customId": 1,
         "children": [
            {
              "nodeName": "A",
              "nodeType": 2,
              "customId": 2,
              "children": [
                {
                  "nodeName": "A2-1.NA61",
                  "nodeType": 3,
                  "customId": 3,
                  "children": [
                    {
                      "nodeName": "A",
                      "nodeType": 4,
                      "customId": 4,
                      "children": [
                        { "nodeName": "A10", "nodeType": 5, "customId": 5 },
                        { "nodeName": "A11", "nodeType": 5, "customId": 6 },
                        { "nodeName": "A01", "nodeType": 5, "customId": 7 },
                        { "nodeName": "A12", "nodeType": 5, "customId": 8 },
                        { "nodeName": "A02", "nodeType": 5, "customId": 9 },
                        { "nodeName": "A13", "nodeType": 5, "customId": 10 },
                        { "nodeName": "A03", "nodeType": 5, "customId": 11 },
                        { "nodeName": "A14", "nodeType": 5, "customId": 12 },
                        { "nodeName": "A04", "nodeType": 5, "customId": 13 },
                        { "nodeName": "A15", "nodeType": 5, "customId": 14 },
                        { "nodeName": "A05", "nodeType": 5, "customId": 15 },
                        { "nodeName": "A16", "nodeType": 5, "customId": 16 },
                        { "nodeName": "A06", "nodeType": 5, "customId": 17 },
                        { "nodeName": "A07", "nodeType": 5, "customId": 18 },
                        { "nodeName": "A08", "nodeType": 5, "customId": 19 },
                        { "nodeName": "A09", "nodeType": 5, "customId": 20 }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    */
    // 根据id获取该节点的所有父节点的对象
    function getParentsId(list, id) {
      for (let i in list) {
        if (list[i].customId == id) {
          return [list[i]];
        }
        if (list[i].children) {
          let node = getParentsId(list[i].children, id);
          if (node !== undefined) {
            return node.concat(list[i]);
          }
        }
      }
    }
    const newArrs = getParentsId(rets, 11);
    console.log('----newArrs---', newArrs);
    
    // 打印数据如下:
    /*
    [
      { nodeName: "A03", nodeType: 5, customId: 11 },
      { nodeName: "A", nodeType: 4, customId: 4, children: []},
      { nodeName: "A2-1.NA61", nodeType: 3, customId: 3, children: []},
      { nodeName: "A", nodeType: 2, customId: 2, children: [] },
      { nodeName: "NA61", nodeType: 1, customId: 1, children: []}
    ]
    */