treemodel

treemodel js - how to get nest level while walking the tree


Api does not provide any information how deep we are in the tree while walking. Do You have any ideas how to get such information?

root.walk(function (node) {
     console.log('Nesting level' + node.??) 
});

Solution

  • You can get the length of the path of the node:

    root.walk(function (node) {
      console.log('Nesting level ' + node.getPath().length);
    });
    

    edit: Efficiency wise, each time you call getPath it follows the parent links up to the root, so it's linear to the node depth.