4

I have an array of objects which goes on something like this

var obj = [{
    id: 23,
    name: 'Test1',
    children: [{
         id: 24,
         name: 'Test2,
         children: [..]
    },{
         id: 25,
         name: 'Test2,
         children: [..]
    }],
},{..}]

Each children can have multiple sub children, so basically I am trying to represent a graph like structure, similar to the output of htmlparser.

I need a function like this:

function(nodeId, json){}

The function will need to find the json object in the tree by using the nodeId and insert the json as a children of that parent object. Thats where I am stuck.

I tried writing a recursive function like this to search the appropriate node, however, the trouble arises when I have to insert the json into the actual obj array.

function findNode(nodeId, json, obj){
    if(obj.id == nodeId){
        obj.children.push(json);
    }
    else{
        for(var i=0; i<obj.children.length; i++){
            findNode(nodeId, json, obj.children[i]);
        }
    }
}

It seems the json gets inserted to the obj local to the recursive function, not the actual root obj. How can I can insert it to the parent obj?

uniquerockrz
  • 231
  • 3
  • 9
  • I don't really understand where you want to insert the json, if you find the child that has the correct id it is under obj.children[i] and the parent is accessible just as obj, or are you looking effectively for obj.parent? – kruczy Feb 24 '15 at 12:19
  • I am finding the parent. If the nodeId matches with the id of the parent, I will insert the json object as the children of that parent – uniquerockrz Feb 24 '15 at 12:20
  • http://stackoverflow.com/questions/2980763/javascript-objects-get-parent this may help – Tachyons Feb 24 '15 at 12:25
  • ok, ill rephrase, say you have a simple tree like A->B->C->D and nodeId matches node C, in which node do you want to put the json? – kruczy Feb 24 '15 at 12:26
  • It will be children of C, i.e. sibling of D – uniquerockrz Feb 24 '15 at 12:27
  • where do you want to push them? – ksg91 Feb 24 '15 at 12:37
  • I have to find the parent node using the nodeid provided, then push the json object into the children array of that parent – uniquerockrz Feb 24 '15 at 12:38
  • your implementation looks fine to me. Are you sure you are not trying to push JSON string? If you want to push JS object, use `JSON.parse(json)` before pushing. – ksg91 Feb 24 '15 at 12:41
  • Another problem with your code is that your `obj` is an Array. It has to be an object for your concept. – ksg91 Feb 24 '15 at 12:51
  • @ksg91 you are right, I have to pass parameter as an object not an array – uniquerockrz Feb 24 '15 at 13:33

2 Answers2

5

First of all, looking at your structure of an object, it looks like structure of your root object is wrong. Root object cannot be an Array of the objects that you intend to have. To take advantage of recursion, you should have structure which is similar on all levels.

You function implementation is almost correct.

I just hope you are not passing JSON string as the json parameter; if you are, you have to parse it and make a JS object by using JSON.parse(json)

Here is updated implementation:

var obj = {
    id: 23,
    name: 'Test1',
    children: [{
         id: 24,
         name: 'Test2',
         children: []
    },{
         id: 25,
         name: 'Test2',
         children: []
    }],
};

var objToBePushed = {
    id: 26,
    name: 'Test3',
    children: [{
         id: 27,
         name: 'Test4',
         children: []
    }]
};

function findNode(nodeId, json, node){
    if(node.id == nodeId){
        node.children.push(json);
    }
    else{

        for(var i=0; i<node.children.length; i++){
            findNode(nodeId, json, node.children[i]);
        }
    }
}

findNode(24, objToBePushed, obj);
console.log(obj);

Working JSFiddle: http://jsfiddle.net/ef3ewoag/2/

ksg91
  • 1,279
  • 14
  • 34
-1

you have to return the object

check this:

http://jsfiddle.net/rf7ysasy/

var obj = [{
    id: 23,
    name: 'Test1',
    children: [{
         id: 24,
         name: 'Test2',
         children: []
    },{
         id: 25,
         name: 'Test2',
         children: []
    }],
}];

var findNode = function(nodeId, json, o){
    if(o.id == nodeId){
        o.children.push(json);
    }
    else{
        if(o.children){
            for(var i=0; i<o.children.length; i++){
                o.children[i] = findNode(nodeId, json, o.children[i]);
            }
        }
    }
    return o;
};

obj = findNode(24,{
         id: 26,
         name: 'Test2',
         children: []
    }, obj);
console.log (obj);