I have an array of objects.
{
c1 : ["a1", "c2"],
c2 : ["b1"],
c3: ["d1"],
b1: ["e"]
d1: ["k"]
}
I need the object to be arranged in a hierarchy. like this,
{
c1: [{a1: null}, {
c2: [{
b1: "e"
}]
}],
c3: [{ d1: "k" }]
}
Note that we can omit the array in last (deepest) key: value pair. This is what I have tried until now.
for (v in hash){
hash[v].forEach(function(ar){
if(hash[ar]){
if (new_hash[v] == undefined){
new_hash[v] = []
}
new_hash[v].push({[ar] : hash[ar]})
}
})
}
I think this problem requires dynamic programming (recursion with saving the state) in which I am not good. Please help.