【问题标题】:Recursively get full path (parents hierarchy) of a given node in a nested object递归获取嵌套对象中给定节点的完整路径(父级层次结构)
【发布时间】:2019-02-13 17:47:40
【问题描述】:

我需要一个打字稿函数,通过传递给定节点的值来递归获取给定节点的完整路径(父层次结构)。

假设我有一个这样的对象数组:

items = [{
        value: 2,
        text: "Name 2",
        children: [{
            value: 7,
            text: "Name 7",
            children: [{
                    value: 10,
                    text: "Name 10",
                    children: []
                },
                {
                    value: 11,
                    text: "Name 11",
                    children: []
                },
                {
                    value: 12,
                    text: "Name 12",
                    children: [{
                        value: 13,
                        text: "Name 13",
                        children: [{
                                value: 14,
                                text: "Name 14",
                                children: []
                            },
                            {
                                value: 15,
                                text: "Name 15",
                                children: []
                            }
                        ]
                    }]
                }
            ]
        }]
    },
    {
        value: 16,
        text: "Name 16",
        children: [{
            value: 17,
            text: "Name 17",
            children: [{
                    value: 18,
                    text: "Name 18",
                    children: []
                },
                {
                    value: 19,
                    text: "Name 19",
                    children: []
                }
            ]
        }]
    }
];

假设我想通过调用一个函数来获取 value=19 的节点的完整路径。

getPath(items, 19);

预期的结果可能是只返回父节点的值

[16, 17, 19]

或对象数组如下:

[{ 值:16, 文本:“名称 16” }, { 值:17, 文本:“姓名 17” }, { 值:19, 文字:“姓名 19” } ]

谢谢,

【问题讨论】:

  • 你的代码出了什么问题?
  • 如果你能分享你已经拥有的代码并指出它在哪里不起作用会很有帮助。
  • @NinaScholz 没有错!我只需要一个函数如何获取给定节点的完整路径。

标签: javascript typescript


【解决方案1】:

希望有帮助

const items = [{
    value: 2,
    text: "Name 2",
    children: [{
        value: 7,
        text: "Name 7",
        children: [{
            value: 10,
            text: "Name 10",
            children: []
        },
        {
            value: 11,
            text: "Name 11",
            children: []
        },
        {
            value: 12,
            text: "Name 12",
            children: [{
                value: 13,
                text: "Name 13",
                children: [{
                    value: 14,
                    text: "Name 14",
                    children: []
                },
                {
                    value: 15,
                    text: "Name 15",
                    children: []
                }
                ]
            }]
        }
        ]
    }]
},
{
    value: 16,
    text: "Name 16",
    children: [{
        value: 17,
        text: "Name 17",
        children: [{
            value: 18,
            text: "Name 18",
            children: []
        },
        {
            value: 19,
            text: "Name 19",
            children: []
        }
        ]
    }]
}
];

function getPath(items, val) {
    for (let i = 0; i < items.length; i++) {
        const item = items[i];
        if (item.value !== val) {
            if (item.children) {
                const path = getPath(item.children, val);
                if (path) {
                    path.unshift(item.value);
                    return path;
                }
            }
        } else {
            return [item.value];
        }
    }
}

console.log(getPath(items, 19));

这是给它的link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多