1

I am trying to save path for each of the node by traversing from root node to the leaf node.

For example, I have nodes like this in hierarchical structure :

Node - 1
     Node-1-1
       Node-1-1-1

Expected output for each node:

So for Node - 1 I would like to have value in property path : /myPath/Node - 1
So for Node-1-1 I would like to have value in property path : /myPath/Node - 1/Node-1-1
So for Node-1-1-1 I would like to have value in property path : /myPath/Node - 1/Node-1-1/Node-1-1-1

But path I am getting is like below with my code for Node-1-1-1:

"/myPath/Node-1-1-1"

But expected out is like below :

/myPath/Node - 1/Node-1-1/Node-1-1-1  (becuase Node-1-1-1 belongs to Node-1-1 but Node-1-1 in turn belongs to Node - 1).

Demo:

var  tree =  JSON.parse('[{"name":"Node","nodes":[{"name":"Node-1","nodes":[{"name":"Node-1-1","nodes":[{"name":"Node-1-1-1","nodes":[],"path":null}],"path":null}],"path":null}],"path":null}]');

traverseTree(tree);
console.log(tree);
 function traverseTree(nodes)
    {
        nodes.forEach(function (node) {
        node.path= getPath(node.name) 
            if (node.nodes) {
                traverseTree(node.nodes);
            }
        });
    }
    
    function getPath(name)
    {
       var path = "/myPath/";
       path = path + name;
       return path;
    }

1 Answer 1

1

You could collect all nodes in the path and use the array then for getting the whole path to the node.

var  tree =  JSON.parse('[{"name":"Node","nodes":[{"name":"Node-1","nodes":[{"name":"Node-1-1","nodes":[{"name":"Node-1-1-1","nodes":[],"path":null}],"path":null}],"path":null}],"path":null}]');

function traverseTree(nodes, path) {
    path = path || [];
    nodes.forEach(function (node) {
        node.path = getPath(path.concat(node.name));
        if (node.nodes) {
            traverseTree(node.nodes, path.concat(node.name));
        }
    });
}
    
function getPath(name) {
   var path = "/myPath/";
   path = path + name.join('/');
   return path;
}

traverseTree(tree);
console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Shorter version with a given prefix

var  tree =  JSON.parse('[{"name":"Node","nodes":[{"name":"Node-1","nodes":[{"name":"Node-1-1","nodes":[{"name":"Node-1-1-1","nodes":[],"path":null}],"path":null}],"path":null}],"path":null}]');

function traverseTree(nodes, path) {
    path = path || [];
    nodes.forEach(function (node) {
        var p = path.concat(node.name);
        node.path = p.join('/')
        if (node.nodes) {
            traverseTree(node.nodes, p);
        }
    });
}

traverseTree(tree, ['', 'myPath']);
console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much mam for your kind efforts towards helping me and please keep helping like this.Once again thank you so much :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.