我想从树中知道先前访问的节点。
尝试下面的例子

var TreeModel = require('tree-model');
tree = new TreeModel();

rootMain = tree.parse({
    id: 1,
    children: [
        {
            id: "11",
            children: [{id: "111"}]
        },
        {
            id: "12",
            children: [{id: "121"}, {id: "122"}]
        },
        {
            id: "13"
        }
    ]
});


如果假设我遍历节点121和122我想要父节点,那么它应该返回12
如果假设我遍历节点111我想要父节点,那么它将返回我11
如果假设我遍历节点13我想要父节点,那么它将返回我1

最佳答案

遍历树时,您可以使用node.parent获取当前节点的父级。

rootMain.walk(node => {
  console.log('node id:', node.model.id);

  if(node.parent) {
    console.log('parent node id:', node.parent.model.id);
  }
});

关于javascript - tree-model-js如何获取先前的 Node ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42360841/

10-10 00:28
查看更多