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

262 Views Asked by At

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.??) 
});
1

There are 1 best solutions below

1
jnuno On BEST ANSWER

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.