I try to use JavaScript to write a recursion function that a new items (array of of objects) to a exist data (unknown size json).
Need some help with that.
The exist data: The data is contain nodes and children.Each children could be more children or node (leaf)
var data = [
{
key:"root",
children:[
{
key:"a0",
children:[]
},
{
key:"a1",
children:[
{
key:"a10",
children:[]
},
{
key:"a11",
children:[]
},
{
key:"a12",
children:[]
}
],
},
{
key:"a1",
children:[]
}
]
}
]
New items I want to add to the exist data:
var result = [
{key:"a1"},
{key:"a12"},
{key:"21"}
]
At the end of adding the new items the data should be like that: The result could be different each time , but always be a simple array of object.
var data = [
{
key:"root",
children:[
{
key:"a0",
children:[]
},
{
key:"a1",
children:[
{
key:"a10",
children:[]
},
{
key:"a11",
children:[]
},
{
key:"a12",
children:[
{key:"21"}
]
}
],
},
{
key:"a1",
children:[]
}
]
}
]
thanks
You can do this with
reduce
method and pass initial array as accumulator.