I am trying to find an Object by ID and remove it. (Of course if this object has subTasks all Objects hanging on this tree are removed too). I have a structure like this (For example), which can grow very big and deep:
[
{
"id": 0,
"lane": 0,
"name": "Task A",
"start": 0,
"end": 10000,
"subTasks": [
{
"id": "0.1",
"lane": 0,
"name": "Subtask",
"start": 0,
"end": 10000,
"class": "danger",
"sublane": 0,
"subTasks": [
{
"id": "0.1.1",
"name": "Subtask",
"start": 0,
"end": 10000,
"subTasks": [
{
"id": "0.1.1.1",
"name": "Subtask",
"start": 0,
"end": 10000,
"subTasks": [
{
"id": "0.1.1.1.1",
"name": "Subtask",
"start": 0,
"end": 10000
},
{
"id": "0.1.1.1.2",
"name": "Subtask",
"start": 0,
"end": 10000
}
]
},
{
"id": "0.1.1.2",
"name": "Subtask",
"start": 0,
"end": 10000
}
]
},
{
"id": "0.1.2",
"name": "Subtask",
"start": 0,
"end": 10000
},
{
"id": "0.1.3",
"name": "Subtask",
"start": 0,
"end": 10000
}
]
},
{
"id": "0.2",
"name": "Subtask",
"start": 0,
"end": 10000
}
],
"class": "danger",
"sublane": 0
},
{
"id": 1,
"lane": 2,
"name": "Task B",
"start": 15000,
"end": 25000,
],
"class": "success",
"sublane": 0
}
]
Now I want to remove the ID = 0.1.1.1.1 for example, but it should work with every other Object the same way, no matter how deep it is nested.
For finding and editing Im using this dfs algorithm:
edit: function (name, start, end) {
for (let obj of gantt.items()) {
result = dfs(obj, id);
if (result) {
result.name = name;
result.start = start;
result.end = end;
}
}
dfs: function (obj, targetId) {
if (obj.id === targetId) {
return obj;
}
if (obj.subTasks) {
for (let item of obj.subTasks) {
let check = dfs(item, targetId);
if (check) {
return check;
}
}
}
return null;
},
But how can I remove/delete the specific Object?
Kind of recursive filter:
const data = [{"id":"0","lane":0,"name":"Task A","start":0,"end":10000,"Subtask":[{"id":"0.1","lane":0,"name":"Subtask","start":0,"end":10000,"class":"danger","sublane":0,"Subtask":[{"id":"0.1.1","name":"Subtask","start":0,"end":10000,"Subtask":[{"id":"0.1.1.1","name":"Subtask","start":0,"end":10000,"Subtask":[{"id":"0.1.1.1.1","name":"Subtask","start":0,"end":10000},{"id":"0.1.1.1.2","name":"Subtask","start":0,"end":10000}]},{"id":"0.1.1.2","name":"Subtask","start":0,"end":10000}]},{"id":"0.1.2","name":"Subtask","start":0,"end":10000},{"id":"0.1.3","name":"Subtask","start":0,"end":10000}]},{"id":"0.2","name":"Subtask","start":0,"end":10000}],"class":"danger","sublane":0},{"id":"1","lane":2,"name":"Task B","start":15000,"end":25000,"class":"success","sublane":0}]; const removeById = (arr, targetId) => arr.reduce((acc, obj) => (obj.id === targetId)? acc: [...acc, {...obj, ...(obj.Subtask && { Subtask: removeById(obj.Subtask, targetId) }) } ], []); console.log(removeById(data, "0.1"));
.as-console-wrapper{min-height: 100%;important: top: 0}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.