I'm facing a challenge where I need to combine and group an array consisting of multiple flat arrays containing only strings, without any objects.
Here's how my array looks like:
var array = [
["MotherNode", "Node1", "ChildNode1", "ChildOfChildNode1"],
["MotherNode", "Node1", "ChildNode2", "ChildOfChildNode2"],
["MotherNode", "Node2", "ChildNode3", "ChildOfChildNode3"],
["MotherNode", "Node2", "ChildNode3", "ChildOfChildNode4"],
["MotherNode", "Node3", "ChildNode4", "ChildOfChildNode5"],
["MotherNode", "Node3", "ChildNode4", "ChildOfChildNode6"]
]
I am working on this task in javascript/angularjs and I believe that utilizing underscore.js' groupBy/combine methods might be the most effective approach. However, existing examples mostly focus on array grouping based on key values within objects, which is slightly different from my scenario. As I am still learning about algorithms, finding a solution on my own seems challenging at this point.
The array I have can potentially contain hundreds of entries, leading to a nested result array with 5-10 levels depth.
If we were to transform the above array, here is the desired outcome:
var result= {
"MotherNode": [{
"Node1":[{
"ChildNode1":"ChildOfChildNode1"
},{
"ChildNode2":"ChildOfChildNode2"
}],
"Node2":[{
"ChildNode3":["ChildOfChildNode3","ChildOfChildNode4"]
}],
"Node3":[{
"ChildNode4":["ChildOfChildNode5","ChildOfChildNode6"]
}]
}
}
If anyone has insights on how to achieve this task, I would greatly appreciate the guidance as I currently feel stuck with no clear direction.