#独家
多个树形结构数组如何合并为一个树结构?

2023-04-24 0 1,042

题目描述

[
    [{
        "folderId": null,
        "folderName": "测试",
        "parentId": null,
        "parentName": null,
        "fileList": [],
        "children": []
    }],
    [{
        "folderId": null,
        "folderName": "测试",
        "parentId": null,
        "parentName": null,
        "fileList": [],
        "children": []
    }],
    [{
        "folderId": null,
        "folderName": "测试",
        "parentId": null,
        "parentName": null,
        "fileList": [],
        "children": [{
            "folderId": null,
            "folderName": "第二层",
            "parentId": null,
            "parentName": "测试",
            "fileList": [],
            "children": []
        }]
    }],
    [{
        "folderId": null,
        "folderName": "测试",
        "parentId": null,
        "parentName": null,
        "fileList": [],
        "children": [{
            "folderId": null,
            "folderName": "第二层",
            "parentId": null,
            "parentName": "测试",
            "fileList": [],
            "children": []
        }]
    }],
    [{
        "folderId": null,
        "folderName": "测试",
        "parentId": null,
        "parentName": null,
        "fileList": [],
        "children": [{
            "folderId": null,
            "folderName": "第二层",
            "parentId": null,
            "parentName": "测试",
            "fileList": [],
            "children": [{
                "folderId": null,
                "folderName": "第三层",
                "parentId": null,
                "parentName": "第二层",
                "fileList": [],
                "children": []
            }]
        }]
    }],
    [{
        "folderId": null,
        "folderName": "测试",
        "parentId": null,
        "parentName": null,
        "fileList": [],
        "children": [{
            "folderId": null,
            "folderName": "第二层",
            "parentId": null,
            "parentName": "测试",
            "fileList": [],
            "children": [{
                "folderId": null,
                "folderName": "测试",
                "parentId": null,
                "parentName": "第二层",
                "fileList": [],
                "children": []
            }]
        }]
    }]
]

希望将上面数组用js转化成下面格式,请问如何实现呢

[
  {
    "folderId": null,
    "folderName": "测试",
    "parentId": null,
    "parentName": null,
    "fileList": [],
    "children": [
      {
        "folderId": null,
        "folderName": "第二层",
        "parentId": null,
        "parentName": "测试",
        "fileList": [],
        "children": [
          {
            "folderId": null,
            "folderName": "第三层",
            "parentId": null,
            "parentName": "第二层",
            "fileList": [],
            "children": []
          },
          {
            "folderId": null,
            "folderName": "测试",
            "parentId": null,
            "parentName": "第二层",
            "fileList": [],
            "children": []
          }
        ]
      }
    ]
  }
]

你可以用递归来实现:

const data = [
  // 这里是你给出的原始数据
];

function mergeTrees(trees) {
  const result = {};
  const uniqueKey = 'folderName';

  function mergeNode(node, resultNode) {
    if (!resultNode.children) {
      resultNode.children = [];
    }

    const existingNode = resultNode.children.find(
      (child) => child[uniqueKey] === node[uniqueKey]
    );

    if (existingNode) {
      node.children.forEach((child) => mergeNode(child, existingNode));
    } else {
      resultNode.children.push(node);
    }
  }

  trees.forEach((tree) => {
    tree.forEach((node) => {
      mergeNode(node, result);
    });
  });

  return result.children;
}

const mergedTree = mergeTrees(data);
console.log(mergedTree);

如果你确定每个数组里面,只包含一个元素(不算子元素),可以用一种简单的方法解决

let str=JSON.stringify(arr) //把你的数组转成json字符串

str=str.replaceAll('[{',"{") //替换字符
str=str.replaceAll('}]',"}")

let obj=JSON.parse(str) //把字符串还原为对象
console.log(obj)
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

1. 有软官网所有资源来源于开发团队,加入会员即可下载使用!如有问题请联系右下角在线客服!
2. 有软官方保障所有软件都通过人工亲测,为每位会员用户提供安全可靠的应用软件、游戏资源下载及程序开发服务。
3. 有软团队针对会员诉求,历经多年拥有现今开发成果, 每款应用程序上线前都经过人工测试无误后提供安装使用,只为会员提供安全原创的应用。
4. PC/移动端应用下载后如遇安装使用问题请联系右下角在线客服或提交工单,一对一指导解决疑难。

有软官网_用软件,找有软 技术分享 多个树形结构数组如何合并为一个树结构? https://www.jiaruvip.com/1647.html

有软应用商店是经过官方安全认证,保障正版软件平台

相关资源

官方客服团队

为您解决烦忧 - 24小时在线 专业服务