我需要一个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”
              }
      ]


谢谢,

最佳答案

希望这个帮助

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

关于javascript - 递归获取嵌套对象中给定节点的完整路径(父级层次结构),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54666781/

10-09 22:49