我有一系列任务,这些任务通过parentId彼此链接。但是,任何任务都可以有多个父母。因此,不一定是简单的单树分层结构。我想要实现的是代替父母[],我想要一个孩子[]。但是我似乎无法理解如何递归地解决这个问题。任何帮助将不胜感激。
这是Flat 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;
};
如果原始数组中的父键等于任务的_id,则此方法很好。但是,我不确定如何将其用于具有对象数组作为值的父键。
答案 0 :(得分:1)
您可以使用_id
作为键的对象,并存储父母及其关系。最后带走没有父母的物品的孩子。
var data = [{ _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: [] }],
tree = function (data, root) {
var t = {};
data.forEach(({ parents, ...o }) => {
Object.assign(t[o._id] = t[o._id] || {}, o);
if (!parents.length) {
t[root] = t[root] || {};
t[root].children = t[root].children || [];
t[root].children.push(t[o._id]);
return;
}
parents.forEach(p => {
Object.assign(t[p._id] = t[p._id] || {}, p);
t[p._id].children = t[p._id].children || [];
t[p._id].children.push(t[o._id]);
});
});
return t[root].children;
}(data);
console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:0)
您可以保留平面JSON数组结构,并仅存储ID以进行交叉引用(id只是示例)
[
{
"_id": "97bbf892-8a2a-4f45-befd-4fdbebded04b",
"task_name": "Task1",
"children": [
"ee78316a-491e-4db5-8f82-13b12b5b86fc",
"a15ca08e-f13b-4d73-a496-ba23832ea233"
],
"parents": [
"ee78316a-491e-4db5-8f82-13b12b5b86fc",
"a15ca08e-f13b-4d73-a496-ba23832ea233"
]
},
{
"_id": "97bbf892-8a2a-4f45-befd-4fdbebded04b",
"task_name": "Task2",
"children": [
"ee78316a-491e-4db5-8f82-13b12b5b86fc",
"a15ca08e-f13b-4d73-a496-ba23832ea233"
],
"parents": [
"ee78316a-491e-4db5-8f82-13b12b5b86fc",
"a15ca08e-f13b-4d73-a496-ba23832ea233"
]
},
{
"_id": "97bbf892-8a2a-4f45-befd-4fdbebded04b",
"task_name": "Task3",
"children": [
"ee78316a-491e-4db5-8f82-13b12b5b86fc",
"a15ca08e-f13b-4d73-a496-ba23832ea233"
],
"parents": [
"ee78316a-491e-4db5-8f82-13b12b5b86fc",
"a15ca08e-f13b-4d73-a496-ba23832ea233"
]
}
]
或者如果您想以当前希望的方式实现它,则可以使用此功能。没有经过完全测试,但您明白了
var tasks = [
{
"_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": []
}
];
function transformTasks(tasks){
var transformed = {};
var transformedAsArray = [];
for(var taskIndex in tasks){
var task = tasks[taskIndex];
if(transformed[task["_id"]] === undefined){
transformed[task["_id"]] = {_id:task["_id"],task_name:task["task_name"],children:[]};
transformedAsArray.push(transformed[task["_id"]]);
}
for(var parentIndex in task["parents"]){
var parentTask = task["parents"][parentIndex];
if(transformed[parentTask["_id"]] === undefined){
transformed[parentTask["_id"]] = {_id:parentTask["_id"],task_name:parentTask["task_name"],children:[]};
transformedAsArray.push(transformed[parentTask["_id"]]);
}
transformed[parentTask["_id"]].children.push(transformed[task["_id"]]);
}
}
return transformedAsArray;
}
console.log(transformTasks(tasks));