I am a little stuck on this. I want to create a tree structure from a flat array. Say I have this input:
var input = [
["a","b","c"],
["a", "b","d"],
["e","f","g"],
];
I want to create a tree structure looking like the following:
// output:
[
{
id: "a",
children: [
{id: "b", children: [
{id: "c", children: []},
{id: "d", children: []}
]},
]
},
{
id: "e",
children: [
{
id: "f",
children: [{ id: "g", children: []}]
},
]
}
]
One way I was thinking of doing this was having a map of all of the parent and iterate through the input array to set the parent to child mappings. But I come to problems when trying to actually construct the tree object from that map and avoiding duplicates. Any pointers are appreciated, thanks!