js 递归树根据子节点获取所有父节点

遍历递归树数组,根据子节点查询父节点中的各个层,并将这些层级合成一个数组返回,没啥可说的,直接上代码吧。

测试数据:

const treeData = [
    {
        key: '1',
        name: '父1',
        children: [
            {
                key: '11',
                name: '子1',
                children: [
                    {
                        key: '111',
                        name: '孙1'
                    },
                    {
                        key: '112',
                        name: '孙2'
                    },
                    {
                        key: '113',
                        name: '孙3'
                    },
                ]
            },
            {
                key: '12',
                name: '子2',
                children: [
                    {
                        key: '121',
                        name: '孙4'
                    },
                    {
                        key: '122',
                        name: '孙5'
                    }
                ]
            },
        ]
    },
    {
        key: '2',
        name: '父2',
        children: [
            {
                key: '21',
                name: '子3',
                children: [
                    {
                        key: '211',
                        name: '孙6'
                    },
                    {
                        key: '212',
                        name: '孙7'
                    },
                    {
                        key: '213',
                        name: '孙8'
                    },
                ]
            }
        ]
    }
]

js代码:

const searchParent = (map, key) => {
    let t = []
    for (let i = 0; i < map.length; i++) {
        const e = map[i]
        if (e.key === key) {
            //若查询到对应的节点,则直接返回
            t.push(e)
            break
        } else if (e.children && e.children.length !== 0) {
            //判断是否还有子节点,若有对子节点进行循环调用
            let p = searchParent(e.children, key)
            //若p的长度不为0,则说明子节点在该节点的children内,记录此节点,并终止循环
            if (p.length !== 0) {
                p.unshift(e)
                t = p
                break
            }
        }
    }
    return t
}

//调用函数方式
searchParent(treeData,'113')

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
JavaScript中,我们可以通过遍历结构来获取某个父节点下的所有子节点。下面是一个使用递归方法来实现的示例代码: ```javascript // 定义结构数据 var data = [ { id: 1, name: '节点1', children: [ { id: 2, name: '节点1-1', children: [] }, { id: 3, name: '节点1-2', children: [ { id: 4, name: '节点1-2-1', children: [] }, { id: 5, name: '节点1-2-2', children: [] } ] } ] }, { id: 6, name: '节点2', children: [] }, { id: 7, name: '节点3', children: [] } ]; // 定义函数来获取指定父节点下的所有子节点 function getChildren(parentNode, result) { for (var i = 0; i < parentNode.children.length; i++) { var childNode = parentNode.children[i]; result.push(childNode); // 将子节点添加到结果数组中 if (childNode.children.length > 0) { getChildren(childNode, result); // 递归调用获取子节点子节点 } } } // 调用函数来获取指定父节点下的所有子节点 var parentId = 1; // 指定父节点的id var parent = data.find(node => node.id === parentId); // 找到指定的父节点 var children = []; // 用于存储子节点的结果数组 getChildren(parent, children); console.log(children); // 输出结果:[{ id: 2, name: '节点1-1', children: [] }, { id: 3, name: '节点1-2', children: [...] }, { id: 4, name: '节点1-2-1', children: [] }, { id: 5, name: '节点1-2-2', children: [] }] ``` 以上代码中,我们首先定义了一个包含结构的数据数组。然后,我们通过定义一个`getChildren`函数,使用递归的方式遍历结构,从指定的父节点开始获取所有子节点。最后,我们调用这个函数来获取指定父节点下的所有子节点,并将结果存储在一个数组中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值