Hi,
I have a JSON tree that contains nodes and children - the format is:
jsonObject =
{
id:nodeid_1,
children: [
{
id:nodeid_2,
children:[]
},
{
id:nodeid_3,
children:[
{
id:nodeid_4,
children:[]
},
{
id:nodeid_5,
children:[]
}
}
}
I don't know the depth of this tree, a node is capable of having many children that also have many children and so on.
My problem is that I need to add nodes into this tree by using a nodeID. For example, a function that can take a nodeID and the node object (including its children), would be able to replace that node within the tree - which as a result would become a bigger tree.
I have only come across recursive functions that allow me to traverse all the nodes within a JSON tree and a modification I have made of one of these functions returns me the node object - but doesn't help me as I need to modify the original tree:
var findNode = {
node:{},
find:function(nodeID,jsonObj) {
if( typeof jsonObj == "object" ) {
$.each(jsonObj, function(k,v) {
if(v == nodeID) {
findNode.node = $(jsonObj).eq(0).toArray()[0];
} else {
findNode.find(nodeID,v);
}
});
} else {
//console.log("jsobObj is not an object");
}
}
}
which allows me to do the following test:
findNode.find("nodeid_3",json);
alert(findNode.node);
So to sum up - how can I modify a value of a JSON tree that has an unknown depth?
Thanks in advance