0

请找树

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。

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

4

1 回答 1

0

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

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

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

var firstSiblingOfNode122 = node122.parent.children[0]
于 2017-12-12T09:43:39.543 回答