【问题标题】:tree-model-js get first siblingstree-model-js 获得第一个兄弟姐妹
【发布时间】:2017-12-12 05:23:17
【问题描述】:

请找到树

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"
        }]
    },
    {id: 2,
    children: [
        {
            id: "21",
            children: [{id: "211"}]
        },
        {
            id: "22",
            children: [{id: "221"}, {id: "222"}]
        },
        {
            id: "23"
        }
    ]});
  1. 假设我在节点 2 上,我想知道它的第一个兄弟节点,所以它应该返回 1。
  2. 第二个我在节点 13 / 12 我想知道它的第一个兄弟节点然后它应该返回 11。
  3. 第二个我在节点 122 上,我想知道它的第一个兄弟节点,然后它应该返回 121。

指导我如何实现这一目标。我尝试了步行并找到方法,但没有运气。

【问题讨论】:

    标签: javascript node.js treemodel


    【解决方案1】:

    首先获取对你所在节点的引用,假设为 122:

    var node122 = rootMain.first(n => n.model.id === "122")
    

    然后获取对父级的引用并获取第一个子级:

    var firstSiblingOfNode122 = node122.parent.children[0]
    

    【讨论】:

      猜你喜欢
      • 2020-05-27
      • 2011-01-09
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      • 2014-07-12
      • 2018-03-16
      • 1970-01-01
      • 2019-05-06
      相关资源
      最近更新 更多