This is more like an algorithmic question. I have two maps: depthMap and ChildMap. DepthMap is a javascript Map initiated with:
let depthMap = new Map()
After processing, I get following data inside the map:
0:[a,b,c,d]
1:[e,f]
2:[g,h,i]
3:[j]
Similarly, I have a childMap which I initialize like:
let childMap = new Map()
After processing, the childMap has following data:
a:[e,f]
b:[]
c:[]
d:[]
e:[g,h]
f:[i]
g:[]
h:[j]
i:[]
j:[]
Now I want to create a nested JSON object out of the depthMap and childMap. I need this dataset for the d3 indented tree. The final JSON should look something like this:
{
name: 'a',
children: [
{
name:e
children: [
{
name: g,
children:[]
},
{
name: h,
children: [..]
}
]
},
{
name: f
children: [
{
name: i
children: []
}
]
}
]
},
...
I am not exactly understanding how can I do this. I was thinking about using the trivial DFS algorithm to parse the graph considering the chilMap as an adjacency matrix.
However, I am not able to figure out how can I implement DFS to create such a JSON.
Any help will be appreciated. I would like to hear approaches using ES6/Javascript.