递归循环树形数组添加元素

递归循环树形数组添加元素

数据结构

let arr = [
        {
            "id": "1367284300238704640",
            "name": "饭堂",
            "category": 1,
            "children": [
                {
                    "id": "1367285230778601472",
                    "name": "学生饭堂",
                    "category": 1,
                    "children": [
                        {
                            "id": "1367287171499839488",
                            "name": "一楼饭堂",
                            "category": 1,
                            "children": [
                                {
                                    "id": "1367287929783865344",
                                    "name": "实中饭堂101",
                                    "category": 1,
                                    "children": [],
                                    "lastChildrenIds": null
                                }
                            ],
                            "lastChildrenIds": null
                        },
                        {
                            "id": "1367287243599925248",
                            "name": "二楼饭堂",
                            "category": 1,
                            "children": [
                                {
                                    "id": "1367295735509438464",
                                    "name": "实中饭堂201",
                                    "category": 1,
                                    "children": [],
                                    "lastChildrenIds": null
                                }
                            ],
                            "lastChildrenIds": null
                        }
                    ],
                    "lastChildrenIds": null
                },
                {
                    "id": "1367285316497592320",
                    "name": "教职工饭堂",
                    "category": 1,
                    "children": [
                        {
                            "id": "1367296370241851392",
                            "name": "教职工饭堂301",
                            "category": 1,
                            "children": [],
                            "lastChildrenIds": null
                        }
                    ],
                    "lastChildrenIds": null
                }
            ],
            "lastChildrenIds": null
        },
        {
            "id": "1367284492132306944",
            "name": "商店汇总",
            "category": 1,
            "children": [
                {
                    "id": "1367285976387440640",
                    "name": "商店",
                    "category": 1,
                    "children": [
                        {
                            "id": "1367287304866123776",
                            "name": "商店1",
                            "category": 1,
                            "children": [
                                {
                                    "id": "1367297031620677632",
                                    "name": "401",
                                    "category": 1,
                                    "children": [],
                                    "lastChildrenIds": null
                                }
                            ],
                            "lastChildrenIds": null
                        },
                        {
                            "id": "1367287346721083392",
                            "name": "新商店",
                            "category": 1,
                            "children": [
                                {
                                    "id": "1367297144363569152",
                                    "name": "501",
                                    "category": 1,
                                    "children": [],
                                    "lastChildrenIds": null
                                }
                            ],
                            "lastChildrenIds": null
                        },
                        {
                            "id": "1367710667522592768",
                            "name": "商店2",
                            "category": 1,
                            "children": [
                                {
                                    "id": "1367711004568473600",
                                    "name": "801",
                                    "category": 1,
                                    "children": [],
                                    "lastChildrenIds": null
                                },
                                {
                                    "id": "1367711038236151808",
                                    "name": "802",
                                    "category": 1,
                                    "children": [],
                                    "lastChildrenIds": null
                                }
                            ],
                            "lastChildrenIds": null
                        }
                    ],
                    "lastChildrenIds": null
                },
                {
                    "id": "1367297446860967936",
                    "name": "水果店",
                    "category": 1,
                    "children": [
                        {
                            "id": "1367297543711641600",
                            "name": "601",
                            "category": 1,
                            "children": [],
                            "lastChildrenIds": null
                        },
                        {
                            "id": "1367297600817090560",
                            "name": "602",
                            "category": 1,
                            "children": [],
                            "lastChildrenIds": null
                        }
                    ],
                    "lastChildrenIds": null
                }
            ],
            "lastChildrenIds": null
        },
        {
            "id": "1367285154148667392",
            "name": "卫生室",
            "category": 1,
            "children": [
                {
                    "id": "1367297642026127360",
                    "name": "701",
                    "category": 1,
                    "children": [],
                    "lastChildrenIds": null
                }
            ],
            "lastChildrenIds": null
        },
        {
            "id": "1371652285871218688",
            "name": "水房",
            "category": 1,
            "children": [
                {
                    "id": "1371652333749198848",
                    "name": "901",
                    "category": 1,
                    "children": [],
                    "lastChildrenIds": null
                }
            ],
            "lastChildrenIds": null
        },
        {
            "id": "1370193940720033792",
            "name": "自助机汇总",
            "category": 1,
            "children": [
                {
                    "id": "1370194264222507008",
                    "name": "自助牛奶机",
                    "category": 1,
                    "children": [],
                    "lastChildrenIds": null
                }
            ],
            "lastChildrenIds": null
        }
    ]

不管有多少个children 都要在对象里插入元素

反正就是自己调用自己 这就是递归了

getOranization() {
    uni.request({
            url: xxx,
            method: 'get',
            data: {},
            header: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            success(res) {
                if (res.data.code == 200) {
                    if (res.data.obj.length > 0) {
                        let arr = []
                        arr = res.data.obj
                        setTimeout(() => {
                            this.department = this.iteration(arr)
                        }, 500);

                    console.log("this.department: ", this.department);
                } else {
                    uni.showToast({
                        title: '暂无组织架构',
                        icon: 'none'
                    })
                }
            } else {

            }
        },
        fail(err) {
            console.log(err);
        }
    })
},

iteration(arr) {
        let newArr = [];
        if (arr != undefined && arr.length > 0) {
            newArr = arr.map(item => {
                item.openTag = false
                item.choose = false
                if (item.children != undefined && item.children.length > 0) {
                    this.iteration(item.children);
                }
                return item;
            });
        }
        return newArr;
},
    
    
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用递归解决树形结构数据循环调用问题的关键是要踪已经访问过的节点,以免进入无限循环。下是一个示例,演示了如何使用递归解决树形结构数据循环调用问题: ```javascript // 树节点类 class TreeNode { constructor(value) { this.value = value; this.children = []; } addChild(childNode) { this.children.push(childNode); } } // 递归遍历树 function traverseTree(node, visitedNodes = new Set()) { // 如果节点已经被访问过,则直接返回 if (visitedNodes.has(node)) { return; } // 访问当前节点 console.log(node.value); visitedNodes.add(node); // 遍历子节点 for (const child of node.children) { traverseTree(child, visitedNodes); } } // 创建树形结构数据 const root = new TreeNode('A'); const nodeB = new TreeNode('B'); const nodeC = new TreeNode('C'); const nodeD = new TreeNode('D'); const nodeE = new TreeNode('E'); root.addChild(nodeB); root.addChild(nodeC); nodeB.addChild(nodeD); nodeD.addChild(nodeE); // 循环调用测试 nodeE.addChild(root); // 在 E 节点中添加对根节点的引用 // 使用递归遍历树 const visitedNodes = new Set(); traverseTree(root, visitedNodes); ``` 在这个示例中,我们定义了一个 `TreeNode` 类表示树的节点,每个节点都有一个值属性 `value` 和一个子节点数组 `children`。我们使用 `addChild` 方法添加子节点。 `traverseTree` 函数用于递归遍历树,并通过一个 `Set` 数据结构来记录已经访问过的节点,以防止无限循环。在访问节点时,我们打印节点的值,并将节点添加到已访问节点集合中。 在创建树形结构数据后,我们对节点 `E` 执行了一个循环调用测试,将根节点 `A` 作为子节点添加到了 `E` 节点。在使用递归遍历树时,我们传入一个空的已访问节点集合,并在遍历过程中传递该集合,确保只访问未被访问过的节点,避免了无限循环。 请注意,以上示例仅提供了一个基本的使用递归解决树形结构数据循环调用问题的思路。在实际应用中,可能需要根据具体情况进行适当的调整和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值