我有一个包含父子关系的对象的平面数组,但它不是很直接。
对象的每个条目都有FromEntityId(Parent)和ToEntityId(Child) 首先,我需要找到父对象或所有父对象,这对象的FromEntityId不存在于任何其他Oject的ToEntityId中,而Children将是该对象的FromEntityId到ToEntityId
以下是OriginalData
OriginalData = [
{
"FromEntityId": 266,
"ToEntityId": 348,
"Status": "NULL"
},
{
"FromEntityId": 266,
"ToEntityId": 353,
"Status": "NULL"
},
{
"FromEntityId": 266,
"ToEntityId": 365,
"Status": "NULL"
},
{
"FromEntityId": 359,
"ToEntityId": 350,
"Status": "NULL"
},
{
"FromEntityId": 359,
"ToEntityId": 354,
"Status": "NULL"
},
{
"FromEntityId": 359,
"ToEntityId": 361,
"Status": "NULL"
},
{
"FromEntityId": 359,
"ToEntityId": 364,
"Status": "NULL"
},
{
"FromEntityId": 359,
"ToEntityId": 372,
"Status": "NULL"
},
{
"FromEntityId": 362,
"ToEntityId": 357,
"Status": "NULL"
},
{
"FromEntityId": 365,
"ToEntityId": 369,
"Status": "NULL"
},
{
"FromEntityId": 369,
"ToEntityId": 670,
"Status": "NULL"
},
{
"FromEntityId": 266,
"ToEntityId": 349,
"Status": "NULL"
},
{
"FromEntityId": 266,
"ToEntityId": 359,
"Status": "NULL"
},
{
"FromEntityId": 350,
"ToEntityId": 351,
"Status": "NULL"
},
{
"FromEntityId": 359,
"ToEntityId": 352,
"Status": "NULL"
},
{
"FromEntityId": 359,
"ToEntityId": 355,
"Status": "NULL"
},
{
"FromEntityId": 359,
"ToEntityId": 362,
"Status": "NULL"
},
{
"FromEntityId": 359,
"ToEntityId": 365,
"Status": "NULL"
},
{
"FromEntityId": 361,
"ToEntityId": 358,
"Status": "NULL"
},
{
"FromEntityId": 273,
"ToEntityId": 356,
"Status": "NULL"
},
{
"FromEntityId": 266,
"ToEntityId": 385,
"Status": "NULL"
},
{
"FromEntityId": 266,
"ToEntityId": 389,
"Status": "NULL"
},
{
"FromEntityId": 266,
"ToEntityId": 388,
"Status": "NULL"
},
{
"FromEntityId": 266,
"ToEntityId": 382,
"Status": "NULL"
},
{
"FromEntityId": 369,
"ToEntityId": 380,
"Status": "NULL"
},
{
"FromEntityId": 273,
"ToEntityId": 381,
"Status": "NULL"
},
{
"FromEntityId": 273,
"ToEntityId": 672,
"Status": "NULL"
}
]
以下是FinalData
finalData = [
{
"FromEntityId": 266,
"Status": "NULL",
"depthLevel": 0,
"children": [
{
"FromEntityId": 266,
"ToEntityId": 348,
"Status": "NULL",
"depthLevel": 1
},
{
"FromEntityId": 266,
"ToEntityId": 353,
"Status": "NULL",
"depthLevel": 1
},
{
"FromEntityId": 266,
"ToEntityId": 365,
"Status": "NULL",
"depthLevel": 1,
"children": [
{
"FromEntityId": 365,
"ToEntityId": 369,
"Status": "NULL",
"depthLevel": 2,
"children": [
{
"FromEntityId": 369,
"ToEntityId": 670,
"Status": "NULL",
"depthLevel": 3
},
{
"FromEntityId": 369,
"ToEntityId": 380,
"Status": "NULL",
"depthLevel": 3
}
]
}
]
},
{
"FromEntityId": 266,
"ToEntityId": 385,
"Status": "NULL",
"depthLevel": 1
},
{
"FromEntityId": 266,
"ToEntityId": 389,
"Status": "NULL",
"depthLevel": 1
},
{
"FromEntityId": 266,
"ToEntityId": 388,
"Status": "NULL",
"depthLevel": 1
},
{
"FromEntityId": 266,
"ToEntityId": 382,
"Status": "NULL",
"depthLevel": 1
},
{
"FromEntityId": 266,
"ToEntityId": 349,
"Status": "NULL",
"depthLevel": 1
},
{
"FromEntityId": 266,
"ToEntityId": 359,
"Status": "NULL",
"depthLevel": 1,
"children": [
{
"FromEntityId": 359,
"ToEntityId": 354,
"Status": "NULL",
"depthLevel": 2
},
{
"FromEntityId": 359,
"ToEntityId": 361,
"Status": "NULL",
"depthLevel": 2,
"children": [
{
"FromEntityId": 361,
"ToEntityId": 358,
"Status": "NULL",
"depthLevel": 3
}
]
},
{
"FromEntityId": 359,
"ToEntityId": 364,
"Status": "NULL",
"depthLevel": 2
},
{
"FromEntityId": 359,
"ToEntityId": 372,
"Status": "NULL",
"depthLevel": 2
},
{
"FromEntityId": 359,
"ToEntityId": 352,
"Status": "NULL",
"depthLevel": 2
},
{
"FromEntityId": 359,
"ToEntityId": 355,
"Status": "NULL",
"depthLevel": 2
},
{
"FromEntityId": 359,
"ToEntityId": 362,
"Status": "NULL",
"depthLevel": 2,
"children": [
{
"FromEntityId": 362,
"ToEntityId": 357,
"Status": "NULL",
"depthLevel": 3
}
]
},
{
"FromEntityId": 359,
"ToEntityId": 365,
"Status": "NULL",
"depthLevel": 2
},
{
"FromEntityId": 359,
"ToEntityId": 350,
"Status": "NULL",
"depthLevel": 2,
"children": [
{
"FromEntityId": 350,
"ToEntityId": 351,
"Status": "NULL",
"depthLevel": 3
}
]
}
]
}
]
},
{
"FromEntityId": 273,
"Status": "NULL",
"depthLevel": 0,
"children": [
{
"FromEntityId": 273,
"ToEntityId": 356,
"Status": "NULL",
"depthLevel": 1
},
{
"FromEntityId": 273,
"ToEntityId": 381,
"Status": "NULL",
"depthLevel": 1
},
{
"FromEntityId": 273,
"ToEntityId": 672,
"Status": "NULL",
"depthLevel": 1
}
]
}
];
我试图在JavaScript中使用Array的reduce方法,但仍然无法实现理想的输出。
答案 0 :(得分:0)
我认为你有更多的逻辑问题。使用子父关系创建一个数组。 F.E.
var cparr = [];
cparr[ichild] = iparent;
然后,如果一个对象没有parrent,则它是一个0深度对象。在新的最终数据对象数组中列出这些对象。第一个对象的所有子对象将作为cildren推送到父对象中,您将在第二个循环中对整个数据进行处理。
你将获得地址从第一个父子数组中放置孩子的地址。
很抱歉,你必须自己做这项工作。
亲切的问候
Kilian的