I have a task data warehouse. Each task also has an array of taskId , on which it depends.
input
let inputArr = [
{
id: 1,
dependOnTasks: [2, 3]
},
{
id: 2,
dependOnTasks: [3]
},
{
id: 3,
dependOnTasks: []
},
{
id: 4,
dependOnTasks: [5]
},
{
id: 5,
dependOnTasks: []
},
{
id: 6,
dependOnTasks: [5]
}
]
The expected result is a grouping of all dependent tasks into one array for display in the user interface.
Output
[
[
{
id: 1,
dependOnTasks: [2, 3]
},
{
id: 2,
dependOnTasks: [3]
},
{
id: 3,
dependOnTasks: []
}
],
[
{
id: 4,
dependOnTasks: [5]
},
{
id: 5,
dependOnTasks: []
},
{
id: 6,
dependOnTasks: [5]
}
]
]
I made a function to do this, but it seems I was thinking wrong by hard-coded it. Hope someone can help me archive it properly using pure JavaScript / TypeScript or Underscore since we already used it in the project.
Note: TaskId will be a random string like "5878465507b36e1f9c4c46fe"