I am struggling hard to add a new item into some nested json given the ID of the parent...
the jsons looks like this
in the angular service I have a method that takes in an ID (from when the button corresponding to a certain ID is clicked)... and I need to somehow traverse through the json and add a child (for right now just some dummy child)
also in the angular service I have the variarable called
public currentlySelectedTree;
which contains the tree that I need to add a child to, within the children list of the passed in ID
how in the hell would I do this SOS... I figure it isn't too hard to traverse through and find it... but how would I add something and persist it?
thank you very much
{
"name":"We Ship Anything Inc - Prod",
"description":"Production",
"id":"1",
"datecreated":"2010-10-10",
"installid":"WeShipAnythingProd",
"showchildren":"1",
"children":[
{
"name":"UAT EU",
"description":"User acceptance testing EU",
"id":"2",
"datecreated":"2018-7-05",
"showchildren":"1",
"children":[
{
"name":"Dev EU 1",
"id":"3",
"description":"Development environment for EU West 1",
"datecreated":"2018-7-10",
"showchildren":"1",
"children":[
]
},
{
"name":"Dev EU 2",
"id":"11",
"description":"Development environment for EU West 2",
"datecreated":"2018-7-11",
"showchildren":"1",
"children":[
]
},
{
"name":"Dev EU 3",
"id":"12",
"description":"Development environment for Mother Russia",
"datecreated":"2018-7-13",
"showchildren":"1",
"children":[
]
}
]
},
{
"name":"UAT US",
"id":"4",
"description":"User acceptance testing US",
"datecreated":"2018-7-12",
"showchildren":"1",
"children":[
{
"name":"Dev US 1",
"description":"Development environment for US East",
"id":"5",
"datecreated":"2018-7-13",
"showchildren":"1",
"children":[
]
},
{
"name":"Dev US 2",
"description":"Development environment for US West",
"id":"13",
"datecreated":"2018-7-13",
"showchildren":"1",
"children":[
]
}
]
}
]
}
current code
addChild(id) {
console.log('add child to ' + id);
console.log(this.currentlySelectedTree[0][0]);
this.traverse_it(this.currentlySelectedTree[0][0], id);
}
traverse_it(obj, id) {
let index = 0;
for (let prop in obj) {
if (typeof obj[prop] === 'object') {
console.log(prop);
this.traverse_it(obj[prop], id);
index = index + 1;
} else {
index = index + 1;
}
}
}