I have the following array:
[{text: "a", depth: 0},
{text: "b", depth: 1},
{text: "c", depth: 2},
{text: "d", depth: 1}]
The problem I'm trying to solve is to take a flattened array (above) and create a nested structure based on each element's depth. The reason I need it nested is to build a list (ordered or unordered) recursively which I can't do with the array I have.
The following is in some way, shape, or form the desired output. The general idea for the nested structure I'm trying to create should be clearer.
{
text: "a",
depth: 0,
sublevel: [
{
text: "a",
depth: 1,
sublevel: [
{
text: "b",
depth: 2,
sublevel: []
}
]
},
{
text: "d",
depth: 1,
sublevel: []
}
]
}