JS 合并树状结构 保留树状结构

树状结构1

const tree1 = [
  {
    id: 1,
    title:1,
    children: [
      {
        id:11,
        title:11,
        children:[]
      },
      {
        id:12,
        children:[
          {
            id: 121,
            children: []
          }
        ]
      }
    ]
  }
]

树状结构2

const tree2 = [
  {
    id: 1,
    children: [
      {
        id: 12,
        children: [
          {
            id: 121,
            children: [
              {
                id: 1211,
                children: []
              }
            ]
          },
          {
            id: 122,
            children:[]
          },
          {
            id: 123,
            children:[]
          }
        ]
      },
      {
        id: 13,
        children: []
      }
    ]
  }
]

期望:

const newTree = [
  {
    id: 1,
    title: 1,
    children: [
      {
        id:11,
        title:11,
        children:[]
      },
      {
        id:12,
        children:[
          {
            id: 121,
            children: [
              {
                id: 1211,
                children: []
              }
            ]
          },
          {
            id: 122,
            children:[]
          },
          {
            id: 123,
            children:[]
          }
        ]
      },
      {
        id: 13,
        children: []
      }
    ]
  }
]

二次更新:

  1. tree1与tree2有着微妙的区别,其中字段不一定相同,是需要糅合在一起
  2. id只为了表示同一节点,不代表上下级之间有长度或大小关联
阅读 2.7k
1 个回答

image.png

interface Node {
    id: number;
    children?: Node[];
}
const mergeNodes = (nodes: Node[]) => {
    const id2Nodes: Record<string, Node[]> = {};
    for (const node of nodes) {
        if (id2Nodes[node.id]) {
            id2Nodes[node.id].push(node);
        } else {
            id2Nodes[node.id] = [node];
        }
    }
    const result: Node[] = [];
    Object.keys(id2Nodes).forEach((id) => {
        result.push({ id: +id, children: mergeNodes(id2Nodes[id].map((o) => o.children ?? []).flat()) });
    });
    return result;
};
const tree1: Node = {
    id: 1,
    children: [
        {
            id: 11,
            children: [],
        },
        {
            id: 12,
            children: [
                {
                    id: 121,
                    children: [],
                },
            ],
        },
    ],
};
const tree2 = {
    id: 1,
    children: [
        {
            id: 12,
            children: [
                {
                    id: 121,
                    children: [
                        {
                            id: 1211,
                            children: [],
                        },
                    ],
                },
                {
                    id: 122,
                    children: [],
                },
                {
                    id: 123,
                    children: [],
                },
            ],
        },
        {
            id: 13,
            children: [],
        },
    ],
};
console.log(mergeNodes([tree1, tree2]));
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题