Reputation: 27
I have an array of tasks that are linked to one-another through parentId's. However any task could have multiple parents. Therefor not necessarily a simple single tree hierarchical structure. What I would like to achieve is instead of parents[], I would like to have a children []. However I cant seem to get my head around how to approach this recursively. Any help would be greatly appreciated.
Here is the Flat JSON array
[
{
"_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": []
}
]
What I would like to achieve is the following
[
{
"_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": []
}
]
}
]
What I have tried
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;
};
This works fine if the parents key in the original array is equal to the _id of the task. However I am not sure how to get this to work for a parent key that has an array of objects as a value.
Upvotes: 1
Views: 384
Reputation: 50
You can keep the the flat JSON array structure and store only Id's for cross referencing like so (ids are just examples)
[
{
"_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"
]
}
]
Or if you want to achieve it the way you currently desire you can use this function. Not completely tested but you get the point
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));
Upvotes: 0
Reputation: 386570
You could take an object with the _id
as key and store the parents as well with their relation. At the end take the children of the items without parents.
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; }
Upvotes: 1