如何基于父子关系生成组织层级输出?代码问题排查
解决受益人父子层级结构生成问题
我看你现在需要把扁平的受益人数据转换成带有层级关系的orgHierarchy结构,之前的代码出现了重复显示businessName和硬编码的问题,咱们来一步步解决这个问题。
首先明确需求:我们要把每个节点按照parentBenePayId的关联,生成树形结构,每个节点包含orgHierarchy(对应businessName)和它的子节点列表。
解决方案思路
- 构建ID映射表:把每个
beneficiaryPayId对应到它的完整对象,这样后续查找子节点时能快速定位,不用反复遍历整个数组。 - 定位根节点:找到所有
parentBenePayId为空的节点,这些是树形结构的顶层节点。 - 递归生成层级:为每个节点生成包含
orgHierarchy和children的结构,children里递归处理该节点的所有子节点。
完整实现代码
var input = { "payBeneficiaries": [{ "beneficiaryPayId": "c946c383-6a64-476a-b903-3bc8baaa716c", "parentBenePayId": "", "beneficiary": { "topId": "06d5e9eb-7110-4c90-aa77-97b3d8ca2c9a", "businessId": "15c58e7d-18dd-49e3-ad86-0bf3f6e5013a", "businessName": "Royal NNJ p.l.c." }, }, { "beneficiaryPayId": "b1da8c12-cb0d-4b0e-8a30-75362dbf73b4", "parentBenePayId": "c946c383-6a64-476a-b903-3bc8baaa716c", "beneficiary": { "topId": "d56eebf3-b97b-429c-ae09-e488d85169cf", "businessId": "a52b0a2d-532b-4fd1-b667-c25691217e5e", "businessName": "Hammes Macejkovic and Steuber" }, }, { "beneficiaryPayId": "f5f04b9b-5711-44d5-b06e-fb525d269310", "parentBenePayId": "c946c383-6a64-476a-b903-3bc8baaa716c", "beneficiary": { "topId": "96737c75-7a7f-4edc-a6b5-cb3149879a78", "businessId": "4e3b061d-5706-45b4-925d-cab675c95d98", "businessName": "Torphy-Osinski" }, }, { "beneficiaryPayId": "2a027e32-ac76-48e4-9ab5-bec130211107", "parentBenePayId": "c946c383-6a64-476a-b903-3bc8baaa716c", "beneficiary": { "topId": "0f9c1849-2c1d-42ae-bdb1-40d83d912171", "businessId": "92ea79f3-ed3f-42e1-8f4c-9e1ccb3d746a", "businessName": "Schinner Inc" }, }, { "beneficiaryPayId": "d0ec2133-fb2f-4cf2-8d2b-3d8d9eed7d09", "parentBenePayId": "f5f04b9b-5711-44d5-b06e-fb525d269310", "beneficiary": { "topId": "54c30c9f-a792-4659-adfc-b4287994cf1e", "businessId": "e6b95c56-ca17-40a4-b5f7-3a0c9fa9b327", "businessName": "Ferry-Donnelly" }, }, { "beneficiaryPayId": "e0b87efe-1bd3-4584-b102-2210ab2bd8a7", "parentBenePayId": "2a027e32-ac76-48e4-9ab5-bec130211107", "beneficiary": { "topId": "b934b36a-0ab5-4869-9220-3bfd90bfb956", "businessId": "c47836bb-87e2-482b-bbf0-d42073c372c6", "businessName": "Lueilwitz-Johns" }, }, { "beneficiaryPayId": "f58d63f6-e823-493a-acf4-226a9c4d21a1", "parentBenePayId": "f5f04b9b-5711-44d5-b06e-fb525d269310", "beneficiary": { "topId": "4e86ab2e-3a2a-4914-a0b3-153439c4013b", "businessId": "aead7de0-f2f3-46ce-a013-f83833b9ec0a", "businessName": "Ward-Collier" }, }, { "beneficiaryPayId": "17263490-9b2b-4b87-a638-cbdaf0cd9daa", "parentBenePayId": "f5f04b9b-5711-44d5-b06e-fb525d269310", "beneficiary": { "topId": "751b8076-d7ca-4cca-922b-aabcc4b39845", "businessId": "6ae1d6e1-9a3b-482b-9c2a-861b3bea0cd4", "businessName": "Nikolaus Dickinson and Wiegand" }, }, { "beneficiaryPayId": "71e36088-724f-4075-be14-5fdb37944447", "parentBenePayId": "b1da8c12-cb0d-4b0e-8a30-75362dbf73b4", "beneficiary": { "topId": "a6c67099-9937-4daa-829f-9b91adfea9a1", "businessId": "dbbb2e12-e50a-4905-93c1-29768b6ed74d", "businessName": "Rath Group" }, }, { "beneficiaryPayId": "d50f34c9-9576-4511-9c49-4fe1fc296a57", "parentBenePayId": "b1da8c12-cb0d-4b0e-8a30-75362dbf73b4", "beneficiary": { "topId": "12e65313-943a-406a-9771-bd8485a35862", "businessId": "70d44d44-f7bf-4fd3-a475-378be757d86f", "businessName": "MacGyver Inc" }, } ] }; // 1. 创建ID到节点的映射表,提升查找效率 const idMap = input.payBeneficiaries.reduce((map, item) => { map[item.beneficiaryPayId] = item; return map; }, {}); // 2. 递归构建层级结构的核心函数 function buildHierarchy(node) { // 筛选当前节点的所有子节点 const children = input.payBeneficiaries .filter(child => child.parentBenePayId === node.beneficiaryPayId) .map(child => buildHierarchy(child)); // 递归处理子节点 // 返回格式化后的节点结构 return { orgHierarchy: node.beneficiary.businessName, children: children.length > 0 ? children : [] }; } // 3. 找到所有根节点并生成完整层级结构 const orgHierarchy = input.payBeneficiaries .filter(node => node.parentBenePayId === "") .map(root => buildHierarchy(root)); // 打印结果 console.log(JSON.stringify(orgHierarchy, null, 2));
代码说明
- ID映射表:用
reduce生成键值对映射,避免重复遍历数组查找父节点,提升代码效率。 - 递归函数:
buildHierarchy会自动处理每个节点的子节点,动态生成层级,完全避免硬编码问题。 - 根节点筛选:确保只从顶层节点开始构建树形结构,符合数据的父子关系逻辑。
输出示例(简化版)
[ { "orgHierarchy": "Royal NNJ p.l.c.", "children": [ { "orgHierarchy": "Hammes Macejkovic and Steuber", "children": [ {"orgHierarchy": "Rath Group", "children": []}, {"orgHierarchy": "MacGyver Inc", "children": []} ] }, { "orgHierarchy": "Torphy-Osinski", "children": [ {"orgHierarchy": "Ferry-Donnelly", "children": []}, {"orgHierarchy": "Ward-Collier", "children": []}, {"orgHierarchy": "Nikolaus Dickinson and Wiegand", "children": []} ] } // 其他子节点省略 ] } ]
这样就完美解决了你之前遇到的重复显示和硬编码问题,不管后续节点数量怎么变化,代码都能正确生成层级结构。
内容的提问来源于stack exchange,提问作者Mohan
相关产品推荐
相关产品推荐

