根据现有的树形结构生成新的树形结构

除了BUG啥也不会
  • 104

test: [

            {
                id: 1,
                domain: '一级域1',
                isSelect: true,
                children: [
                    {
                        id: 11,
                        domain: '二级域11',
                        parentid: 1,
                        isSelect: true,
                        children: [
                            {
                                id: 111,
                                domain: '三级域1121',
                                parentid: 11,
                                isSelect: true,
                                children: [
                                    {
                                        id: 1111,
                                        isSelect: true,
                                        parentid: 111,
                                        domain: '四级域111',
                                    },
                                ],
                            },
                            {
                                id: 111,
                                domain: '三级域1311',
                                parentid: 11,
                                isSelect: true,
                                children: [
                                    {
                                        id: 1111,
                                        isSelect: true,
                                        parentid: 111,
                                        domain: '四级域111',
                                    },
                                ],
                            },
                            {
                                id: 111,
                                domain: '三级域1411',
                                parentid: 11,
                                children: [{ id: 1111, parentid: 111, domain: '四级域111' }],
                            },
                            {
                                id: 111,
                                domain: '三级域1511',
                                parentid: 11,
                                children: [{ id: 1111, parentid: 111, domain: '四级域111' }],
                            },
                            {
                                id: 111,
                                domain: '三级域1611',
                                parentid: 11,
                                children: [{ id: 1111, parentid: 111, domain: '四级域111' }],
                            },
                            {
                                id: 111,
                                domain: '三级域1711',
                                parentid: 11,
                                children: [{ id: 1111, parentid: 111, domain: '四级域111' }],
                            },
                            {
                                id: 111,
                                domain: '三级域1811',
                                parentid: 11,
                                children: [{ id: 1111, parentid: 111, domain: '四级域111' }],
                            },
                            {
                                id: 111,
                                domain: '三级域1911',
                                parentid: 11,
                                children: [{ id: 1111, parentid: 111, domain: '四级域111' }],
                            },
                            {
                                id: 111,
                                domain: '三级域1110',
                                parentid: 11,
                                children: [{ id: 1111, parentid: 111, domain: '四级域111' }],
                            },
                            {
                                id: 111,
                                domain: '三级域11111',
                                parentid: 11,
                                children: [{ id: 1111, parentid: 111, domain: '四级域111' }],
                            },
                        ],
                    },
                    {
                        id: 12,
          domain: '二级域12',
          isSelect: true,
                        children: [
                            {
                                id: 112,
                                domain: '三级域112',
              parentid: 12,
              isSelect: true,
                                children: [{ id: 1111, parentid: 112, domain: '四级域1111' }],
                            },
                        ],
                    },
                ],
            },
            {
                id: 2,
      domain: '一级域2',
      isSelect: true,
                children: [
                    {
                        id: 21,
                        domain: '二级域21',
          parentid: 2,
          isSelect: true,
                        children: [{ id: 211,        isSelect: true, parentid: 21, domain: '三级域211' }],
                    },
                    {
                        id: 22,
                        domain: '二级域22',
                        parentid: 2,
                        children: [{ id: 212, parentid: 21, domain: '三级域212' }],
                    },
                ],
            },
            {
                id: 3,
                domain: '一级域3',
                children: [
                    {
                        id: 31,
                        domain: '二级域31',
                        parentid: 3,
                        children: [{ id: 311, parentid: 31, domain: '三级域311' }],
                    },
                    {
                        id: 32,
                        domain: '二级域32',
                        parentid: 3,
                        children: [{ id: 312, parentid: 31, domain: '三级域312' }],
                    },
                ],
            },
            {
                id: 4,
                domain: '一级域4',
                children: [
                    {
                        id: 41,
                        parentid: 4,
                        domain: '二级域41',
                    },
                    {
                        id: 42,
                        parentid: 4,

                        domain: '二级域42',
                    },
                ],
            },
            {
                id: 5,
                domain: '一级域5',
            },
        ],
        stepList: [
            {
                title: 'a',
                subTitle: '1',
            },
            {
                title: 'b',
                subTitle: '2',
            },
            {
                title: 'c',
                subTitle: '3',
            },
        ]

根据有isSelect: true的字段生成新的树形结构,

回复
阅读 942
2 个回答

const getSelectedSubTree = (nodes) => {
  const dummyRoot = {
    children: []
  };

  const dfs = (vertex, origin) => {
    if (vertex && vertex.isSelect) {
      if (!origin.children) origin.children = [];

      const newVertex = { ...vertex, children: [] };
      origin.children.push(newVertex);

      if (!vertex.children) return;

      for (let i = 0; i < vertex.children.length; ++i) {
        dfs(vertex.children[i], newVertex);
      }
    }
  };

  for (let i = 0; i < nodes.length; ++i) {
    dfs(nodes[i], dummyRoot);
  }

  return dummyRoot.children;
};

console.log(JSON.stringify(getSelectedSubTree(temp), null, 2));
小丑的悲伤
  • 3
新手上路,请多包涵

看一下这个是你想要的效果吗

const ergodicData = dataArr => {
    let arr = []
    for (let i = 0; i < dataArr.length; i++) {
      const item = dataArr[i]
      if (item.isSelect) {
        let obj = { ...item }
        item.children && (obj.children = ergodicData(item.children))
        arr.push(obj)
        item.children && ergodicData(obj.children)
      }
    }
    return arr
  }
  // 主方法
  const enforce = () => {
    let data = ergodicData(treeData)
    console.log(data)
  }
  enforce()
宣传栏