【递归汇总】前端各种递归方法记录合集

递归(传入数组,把每一个对象内的id保存到一个数组内返回出去)

getAllId(keys, dataList) {
                if (dataList && dataList.length) {
                    for (let i = 0; i < dataList.length; i++) {
                        keys.push(dataList[i].id)
                        if (dataList[i].children) {
                            keys = this.getAllId(keys, dataList[i].children)
                        }
                    }
                }
                return keys
            },
        }

递归(传入数组和对比值,返回对应id的子级数组)

findChildrenById(arr, id) {
                for (let i = 0; i < arr.length; i++) {
                    if (arr[i].id === id) {
                        return arr[i].children || [];
                    }
                    if (Array.isArray(arr[i].children)) {
                        const result = this.findChildrenById(arr[i].children, id);
                        if (result.length > 0) {
                            return result;
                        }
                    }
                }
                return [];
            },

递归(传入id,数组)用于你传入一个tree结构的数据子级的id或者pid。然后匹配找到所有父节点的id返回给你。如果点击的就是顶级了,没有父级,就会返回false。

findP(id, list = [], result = []) {
                for (let i = 0; i < list.length; i += 1) {
                    console.log('*******', list[i].id, '********')
                    const item = list[i]
                    // 找到目标
                    if (item.id === id) {
                        console.log('找到了')
                        // 加入到结果中
                        result.push(item.id)
                        // 因为可能在第一层就找到了结果,直接返回当前结果
                        if (result.length === 1) return result
                        return true
                    }
                    // 如果存在下级节点,则继续遍历
                    if (item.children) {
                        // 预设本次是需要的节点并加入到最终结果result中
                        result.push(item.id)
                        const find = this.findP(id, item.children, result)
                        // 如果不是false则表示找到了,直接return,结束递归
                        if (find) {
                            return result
                        }
                        // 到这里,意味着本次并不是需要的节点,则在result中移除
                        result.pop()
                    }
                }
                // 如果都走到这儿了,也就是本轮遍历children没找到,将此次标记为false
                return false
            },

递归:传入ID和数组,返回对应id的所有父级,格式:[1,2,3]

    getActiveMenuId(path, arr, paths) {
        if (paths === undefined) {
            paths = []
        }
        if(arr){
          for (let i = 0; i < arr.length; i++) {
              const tmpPath = paths.concat()
              tmpPath.push(arr[i].id)
              if (path === arr[i].id) {
                  return tmpPath
              }
              if (arr[i].children !== null) {
                  const findResult = this.getActiveMenuId(path, arr[i].children, tmpPath)
                  if (findResult) {
                      return findResult
                  }
              }
          }
        }
    },

递归:找到符合条件的数据删除

arr:数组
key:需要传入的判断条件

removeObjects(arr, key) {
                // 数组不存在不执行
                if (!arr) {
                    return
                }
                // 遍历数组  
                for (let i = 0; i < arr.length; i++) {
                    // 检查当前对象是否符合条件  
                    if (arr[i].key == key) {
                        // 删除当前对象  
                        arr.splice(i, 1);
                        // 由于已经删除了一个对象,所以需要调整索引以避免跳过下一个对象  
                        i--;
                    } else {
                        // 如果当前对象不符合条件,则递归检查其子对象  
                        this.removeObjects(arr[i].children, key);
                    }
                }
            },

传入id和树形结构数据,id一致的返回对应的节点数据

findInTree(id, tree) {  
                // 遍历树中的每个节点  
                for (let i = 0; i < tree.length; i++) {  
                    const node = tree[i];  
                    // 如果当前节点的ID与给定的ID匹配,返回该节点  
                    if (node.id === id) {  
                    return node;  
                    }  
                    // 如果当前节点有子节点,递归地在子树中查找  
                    if (node.children) {  
                        const result = this.findInTree(id, node.children);  
                        if (result) {  
                            return result;  
                        }  
                    }  
                }  
                // 如果在树中没有找到匹配的节点,返回null  
                return null;  
            },
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

接口写好了吗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值