树形结构获取所有直属父级节点

递归获取

let arr = [
    {
        name: "/",
        meta: {},
        children: [
            {
                name: "home",
            },
            {
                name: "home2",
            },
            {
                name: "common-components",
                children: [
                    {
                        name: "form-component",
                    }
                ]
            },
            {
                name: "multilevel-menu",
                children: [
                    {
                        name: "second-menu",
                        children: [
                            {
                                name: "third-menu",
                            }
                        ]
                    }
                ]
            }
        ]
    }
];

// 递归实现
function findParents(tree, targetName, parents = []) {
    for (let node of tree) {
        if (node.name === targetName) {
            return parents.concat(node);
        }
        if (node.children) {
            let found = findParents(node.children, targetName, parents.concat(node));
            if (found) {
                return found;
            }
        }
    }
    return null;
}
let targetName = "third-menu";
let parents = findParents(arr, targetName);

if (parents) {
    console.log("归属的父级信息(从树的顶层到当前层级):", parents.reverse());
} else {
    console.log("未找到归属的父级信息。");
}

在这里插入图片描述

尾递归获取

let arr = [
    {
        name: "/",
        meta: {},
        children: [
            {
                name: "home",
            },
            {
                name: "home2",
            },
            {
                name: "common-components",
                children: [
                    {
                        name: "form-component",
                    }
                ]
            },
            {
                name: "multilevel-menu",
                children: [
                    {
                        name: "second-menu",
                        children: [
                            {
                                name: "third-menu",
                            }
                        ]
                    }
                ]
            }
        ]
    }
];

function findParentsTailRecursive(tree, targetName, parents = [], index = 0) {
    if (index >= tree.length) {
        return null;
    }

    const currentNode = tree[index];
    const updatedParents = parents.concat(currentNode);

    if (currentNode.name === targetName) {
        return updatedParents;
    }

    if (currentNode.children) {
        const result = findParentsTailRecursive(currentNode.children, targetName, updatedParents);
        if (result) {
            return result; // 这里直接返回递归调用的结果
        }
    }

    return findParentsTailRecursive(tree, targetName, parents, index + 1);
}

let targetName = "third-menu";
let parents = findParentsTailRecursive(arr, targetName);

if (parents) {
    console.log("归属的父级信息(从树的顶层到当前层级):", parents);
} else {
    console.log("未找到归属的父级信息。");
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值