递归获取嵌套对象中给定节点的完整路径(父级层次结构)

时间:2019-02-13 09:34:30

标签: javascript typescript

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

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

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”               }       ]

谢谢

1 个答案:

答案 0 :(得分: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