0

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?

2
  • 1
    You can set the parent array of the element you want to remove to a new array without this value. For this, you could use the Array.filter(...) method. Commented Jan 28, 2022 at 13:59
  • It is a recursive find Commented Jan 28, 2022 at 14:03

1 Answer 1

3

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}

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.