如何基于ParentID字段转换对象数组的格式?
基于ParentID字段转换对象数组格式
需求说明
- ParentID为0的对象作为父对象,必须添加字符串类型的
children字段 - ParentID不为0的对象,作为
pID与其ParentID匹配的父对象的子元素,存入父对象的children字段 - 父对象无子女时,
children字段为空字符串
示例输入数据
var JsonVal = { "data": { "Factorid": 197325, "orders": [ { "pID": 794522, "Count": 1, "ParentID": 794551, "Description": "", "productid": "1428539", "UnitPrice": "300000" }, { "pID": 794525, "Count": 1, "ParentID": 794551, "Description": "", "productid": "1428543", "UnitPrice": "600000" }, { "pID": 794550, "Count": 2, "ParentID": 0, "Description": "", "productid": "1428648", "UnitPrice": "60000" }, { "pID": 794551, "Count": 1, "ParentID": 0, "Description": "", "productid": "1428647", "UnitPrice": "250000" }, { "pID": 794526, "Count": 3, "ParentID": 794550, "Description": "", "productid": "1428548", "UnitPrice": "600000" }, { "pID": 794527, "Count": 1, "ParentID": 0, "Description": "", "productid": "1428542", "UnitPrice": "400000" } ] } };
期望输出格式
{ "data": { "Factorid": 197325, "orders": [ { "pID": 794550, "Count": 2, "ParentID": 0, "Description": "", "productid": "1428648", "UnitPrice": "60000", "children": "[{ 'pID': 794526, 'Count':3, 'ParentID': 794550, 'Description': '', 'productid': '1428548', 'UnitPrice': '600000' }]" }, { "pID": 794551, "Count": 1, "ParentID": 0, "Description": "", "productid": "1428647", "UnitPrice": "250000", "children": "[ { 'pID': 794522, 'Count': 1, 'ParentID': 794551, 'Description': '', 'productid': '1428539', 'UnitPrice': '300000' }, { 'pID': 794525, 'Count': 1, 'ParentID': 794551, 'Description': '', 'productid': '1428543', 'UnitPrice': '600000' } ]" }, { "pID": 794527, "Count": 1, "ParentID": 0, "Description": "", "productid": "1428542", "UnitPrice": "400000", "children": "" } ] } }
你的现有代码
JsonVal.orders.forEach(orders => { if(orders.ParentID == 0){ orders.children=""; }else{ if(orders.ParentID == new_rows){ JsonVal.orders = JsonVal.orders.filter(item => item.ParentID == orders.ParentID); } } });
解决方案代码
下面是实现需求的完整代码,步骤清晰可复用:
// 1. 创建父对象映射表,用pID作为键,快速查找父对象 const parentMap = {}; // 2. 筛选所有父对象,初始化children为空字符串并存入映射表 const parentOrders = JsonVal.data.orders.filter(order => { if (order.ParentID === 0) { order.children = ""; parentMap[order.pID] = order; return true; } return false; }); // 3. 遍历子对象,添加到对应父对象的临时数组中 JsonVal.data.orders.forEach(order => { if (order.ParentID !== 0 && parentMap[order.ParentID]) { if (!parentMap[order.ParentID]._tempChildren) { parentMap[order.ParentID]._tempChildren = []; } parentMap[order.ParentID]._tempChildren.push(order); } }); // 4. 将临时数组转为字符串(匹配示例的单引号格式),清理临时属性 parentOrders.forEach(parent => { if (parent._tempChildren) { parent.children = JSON.stringify(parent._tempChildren).replace(/"/g, "'"); delete parent._tempChildren; } else { parent.children = ""; } }); // 5. 替换原数组为处理后的父对象数组 JsonVal.data.orders = parentOrders; // 查看结果 console.log(JSON.stringify(JsonVal, null, 2));
代码说明
- 用
parentMap快速定位父对象,避免嵌套循环,提升处理效率 - 先筛选父对象并初始化
children,确保所有父对象都符合字段要求 - 临时属性
_tempChildren用于暂存子对象,最后统一转换为字符串格式 - 替换双引号为单引号是为了匹配示例输出,若不需要标准JSON格式外的特殊处理,可去掉
.replace(/"/g, "'")
内容的提问来源于stack exchange,提问作者Sahar
相关产品推荐
相关产品推荐

