You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将对象数组转换为树形图(Tree Map)所需的层级数据结构

将扁平化数组转换为树形层级结构

我有一组扁平化的对象数组,需要将其转换为树形图所需的层级数据结构,具体如下:

原始数据

[
    {
      "hospital": "hospital 1",
      "drg": "81",
      "Percentage": 96.10584123814279
    },
    {
      "hospital": "hospital 2",
      "drg": "80",
      "Percentage": 96.66666666666667,
      "Incidents": 2.0
    },
    {
      "hospital": "hospital 1",
      "drg": "82",
      "Percentage": 97.1736204576043
    },
    {
      "hospital": "hospital 3",
      "drg": "82",
      "Percentage": 96.58119658119658
    },
    {
      "hospital": "hospital 3",
      "drg": "80",
      "Percentage": 95.83333333333334,
      "Incidents": 3.0
    },
    {
      "hospital": "hospital 3",
      "drg": "81",
      "Percentage": 95.84885577434807
    },
    {
      "hospital": "hospital 4",
      "drg": "82",
      "Percentage": 94.91017964071857,
      "Incidents": 1.0
    },
    {
      "hospital": "hospital 2",
      "drg": "82",
      "Percentage": 95.53072625698324
    },
    {
      "hospital": "hospital 4",
      "drg": "81",
      " Percentage": 95.65987599645705
    },
    {
      "hospital": "hospital 4",
      "drg": "80",
      "Percentage": 96.18320610687023,
      "Incidents": 2.0
    },
    {
      "hospital": "hospital 1",
      "drg": "80",
      "Percentage": 93.5064935064935,
      "Incidents": 4.0
    },
    {
      "hospital": "hospital 2",
      "drg": "81",
      "Percentage": 95.24096385542168
    }
]

预期结果

[
{
    "hospital": "hospital 1",
     "children" :[
        {"drg": "81","Percentage": 96.10584123814279},
        {"drg": "82","Percentage": 97.1736204576043},
        {"drg": "80","Percentage": 93.5064935064935}
      ]
    },
  {
    "hospital": "hospital 2",
    "children" :[
        {"drg": "81","Percentage": 95.24096385542168},
        {"drg": "82","Percentage": 95.53072625698324},
        {"drg": "80","Percentage": 96.66666666666667}
      ]
  },
    {
    "hospital": "hospital 3",
    "children" :[
        {"drg": "82","Percentage": 96.58119658119658},
        {"drg": "80","Percentage": 95.83333333333334},
        {"drg": "81","Percentage": 95.84885577434807}
      ]
  },
    {
    "hospital": "hospital 4",
    "children" :[
        {"drg": "82","Percentage": 94.91017964071857},
        {"drg": "81"," Percentage": 95.65987599645705},
        {"drg": "80","Percentage": 96.18320610687023}
      ]
  }
]

解决方案

可以通过遍历原始数组,利用对象映射表分组数据,快速构建树形结构:

function flattenToTree(data) {
    const hospitalMap = {};
    const treeResult = [];

    data.forEach(item => {
        const hospital = item.hospital;
        // 提取子节点数据,保留除hospital外的所有字段
        const childNode = {
            drg: item.drg,
            ...('Percentage' in item && { Percentage: item.Percentage }),
            ...(' Percentage' in item && { ' Percentage': item[' Percentage'] }),
            ...('Incidents' in item && { Incidents: item.Incidents })
        };

        // 若当前医院未在映射表中,创建新节点并加入结果数组
        if (!hospitalMap[hospital]) {
            hospitalMap[hospital] = {
                hospital: hospital,
                children: []
            };
            treeResult.push(hospitalMap[hospital]);
        }

        // 将子节点添加到对应医院的children数组
        hospitalMap[hospital].children.push(childNode);
    });

    return treeResult;
}

// 调用示例
const rawData = [/* 插入上述原始数据数组 */];
const treeData = flattenToTree(rawData);
console.log(JSON.stringify(treeData, null, 2));

核心逻辑说明

  • 映射表去重:用hospitalMap存储每个医院对应的树形节点,避免重复创建相同医院的父节点。
  • 子节点提取:遍历每条数据时,提取除hospital外的所有字段作为子节点内容,兼容原始数据中带空格的 Percentage字段。
  • 动态构建树形:检查映射表,不存在则创建父节点并加入结果数组,再将子节点追加到对应父节点的children数组中。

内容的提问来源于stack exchange,提问作者Bhabagrahi Behera

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 09:20:39