Convert following json in tree structure
data = {
"a": {
"b": [
{
"c": "v1",
"f": [
"v3",
"v4"
]
}
]
}
}
Actually I want to use angular2 tree package to display the above json in tree level so Expected output should be
nodes = [
{
"name": "a",
"children": [
{
"name": "b",
"children": [
{
"name": "c"
},
{
"name": "f",
"children": [
{
"name": "v3"
},
{
"name": "v4"
}
]
}
]
}
]
}
]
I'm trying following but not work to convert above json in tree level
const getFinalNode = function(data, nodes){
if (typeof data === 'object'){
Object.keys(data).forEach(function(key){
let tmpObj = {
name: key,
children: []
}
if(typeof key === "object"){
tmpObj.children = getFinalNode(data[key])
}
nodes.push(tmpObj)
})
}elseif(data instanceof Array){
arr.forEach(function(obj, idx){
let tmpObj = {
name: obj,
children: []
}
if(data instanceof Array){
tmpObj.children = getFinalNode(obj.children)
}
arr[idx] = tmpObj
})
return arr
}
return nodes
}
getFinalNode(data, nodes);
Actually I want to display json in following way.