【发布时间】:2019-12-02 10:05:18
【问题描述】:
我有一组通过 parentId 相互链接的任务。但是,任何任务都可能有多个父级。因此不一定是简单的单树层次结构。我想要实现的是代替父母[],我想有一个孩子[]。但是,我似乎无法理解如何递归地处理这个问题。任何帮助将不胜感激。
这是平面 JSON 数组
[
{
"_id": "4b04e450-06d5-4453-8d50-d3b2a70d9b2d",
"task_name": "Parent2",
"parents": []
},
{
"_id": "a15ca08e-f13b-4d73-a496-ba23832ea233",
"task_name": "Endpoints",
"parents": [
{
"_id": "97bbf892-8a2a-4f45-befd-4fdbebded04b",
"task_name": "Parent1"
},
{
"_id": "4b04e450-06d5-4453-8d50-d3b2a70d9b2d",
"task_name": "Parent2"
}
]
},
{
"_id": "ee78316a-491e-4db5-8f82-13b12b5b86fc",
"task_name": "Mapping",
"parents": [
{
"_id": "97bbf892-8a2a-4f45-befd-4fdbebded04b",
"task_name": "Parent1"
}
]
},
{
"_id": "97bbf892-8a2a-4f45-befd-4fdbebded04b",
"task_name": "Parent1",
"parents": []
}
]
我想要实现的是以下
[
{
"_id": "97bbf892-8a2a-4f45-befd-4fdbebded04b",
"task_name": "Parent1",
"children": [
{
"_id": "ee78316a-491e-4db5-8f82-13b12b5b86fc",
"task_name": "Mapping",
"children": []
},
{
"_id": "a15ca08e-f13b-4d73-a496-ba23832ea233",
"task_name": "Endpoints",
"children": []
}
]
},
{
"_id": "4b04e450-06d5-4453-8d50-d3b2a70d9b2d",
"task_name": "Parent2",
"children": [
{
"_id": "a15ca08e-f13b-4d73-a496-ba23832ea233",
"task_name": "Endpoints",
"children": []
}
]
}
]
我尝试过的
function transform(list, idAttr, parentAttr, childrenAttr) {
if (!idAttr) idAttr = '_id';
if (!parentAttr) parentAttr = 'parents';
if (!childrenAttr) childrenAttr = 'children';
var newArr = [];
var lookup = {};
list.forEach(function(obj) {
lookup[obj[idAttr]] = obj;
obj[childrenAttr] = [];
});
list.forEach(function(obj) {
if (obj[parentAttr] != null) {
lookup[obj[parentAttr]][childrenAttr].push(obj);
} else {
newArr.push(obj);
}
});
return newArr;
};
如果原始数组中的 parent 键等于任务的 _id,则此方法可以正常工作。但是,我不确定如何让它适用于具有对象数组作为值的父键。
【问题讨论】:
-
嗨!请使用tour(您将获得徽章!)并通读help center,尤其是How do I ask a good question? 您最好的选择是进行研究,search 以获取有关 SO 的相关主题,然后试一试. 如果您在进行更多研究和搜索后遇到困难并且无法摆脱困境,请发布您的尝试minimal reproducible example,并具体说明您遇到的问题。人们会很乐意提供帮助。
-
您将如何实现这一目标?基于什么 ?不清楚。
-
请发布您的解决方案方法,该方法可以修改为工作版本,以查看您的基础和您想要实现的目标。
-
为什么一个节点有两个父节点?我的意思是实际上是的,但作为一个抽象的数据结构?第二个父母要去哪里,孩子们会发生什么?
-
@J.Knabenschuh 我已经用我的“解决方案”努力更新了我的问题。
标签: javascript arrays parent-child