当前位置: 首页 > news >正文

原生js vue react通用的递归函数

c31bb4314f4144bbb11cf5a0a93bec2f.gif

🙂博主:锅盖哒
🙂文章核心:原生js vue react通用的递归函数

目录大纲

1.递归函数的由来

 2.代码逻辑


1.递归函数的由来

        递归函数的由来可以追溯到数学中的递归概念和数学归纳法。

        在数学中,递归是指通过定义基本情况和递推公式,将一个问题分解为更简单的、与原问题具有相同结构的子问题,并用子问题的解来构建原问题的解。递归的思想在解决一些数学问题时非常有用。

        当计算机科学发展起来后,人们将递归思想应用到程序设计中,形成了递归函数的概念。递归函数是一种能够调用自身的函数。 递归函数的使用可以有效地解决一些需要重复执行相似任务或处理具有递归结构的问题。

递归函数的设计通常包括两部分:

  1. 基本情况(边界条件):定义问题的最简单情况和对应的解,作为递归的终止条件。
  2. 递推公式(递归关系):描述问题和子问题之间的联系,通过递归调用函数本身来解决规模较小的子问题。

         递归函数的执行过程中,每次调用都会创建一个新的函数上下文(包括参数、局部变量等),并将其压入执行栈。递归函数通过不断调用自身,并处理更小规模的子问题,直到达到基本情况为止,然后逐步返回结果,并依次弹出执行栈中的上下文,完成整个递归过程。

递归函数在实际编程中有广泛的应用,例如树的遍历、图的搜索、排列组合、动态规划等领域。但需要注意的是,递归函数的设计要合理,避免无限递归或重复计算,以确保程序的正确性和效率。

 2.代码逻辑

假设下方的数据是我们从接口中获取到的

var temp = [{"id": 1,"pid": 0,"title": "一档","money": "666.00"},{"id": 2,"pid": 0,"title": "二档","money": "1900.00"},{"id": 3,"pid": 1,"title": "一档1级","money": "1800.00"},{"id": 4,"pid": 0,"title": "三档","money": "2000.00"},{"id": 5,"pid": 0,"title": "四档","money": "2200.00"},{"id": 6,"pid": 5,"title": "四档1级","money": "2200.00"},{"id": 7,"pid": 2,"title": "二档1级","money": "3700.00"},{"id": 8,"pid": 4,"title": "三档1级","money": "2000.00"},{"id": 10,"pid": 0,"title": "五档","money": "250.00"},{"id": 12,"pid": 10,"title": "五档1级","money": "10000.00"},{"id": 18,"pid": 1,"title": "一档2级","money": "1800.00"},{"id": 21,"pid": 0,"title": "六档","money": "0.00"},{"id": 24,"pid": 1,"title": "一档3级","money": "1800.00"},{"id": 25,"pid": 2,"title": "二档2级","money": "4100.00"},{"id": 26,"pid": 2,"title": "二档3级","money": "4400.00"},{"id": 27,"pid": 4,"title": "三档2级","money": "2000.00"},{"id": 28,"pid": 4,"title": "三档3级","money": "2000.00"},{"id": 29,"pid": 5,"title": "四档2级","money": "2200.00"},{"id": 30,"pid": 5,"title": "四档3级","money": "2200.00"},{"id": 31,"pid": 2,"title": "1","money": "4100.00"},{"id": 32,"pid": 2,"title": "3","money": "4400.00"},{"id": 38,"pid": 0,"title": "八档","money": "0.00"},{"id": 39,"pid": 10,"title": "轻微","money": "100000000.00"},{"id": 44,"pid": 42,"title": "一级","money": "12323.00"},{"id": 45,"pid": 42,"title": "二级","money": "755.00"},{"id": 47,"pid": 42,"title": "三级","money": "44444.00"},{"id": 49,"pid": 48,"title": "一级","money": "21234.00"},{"id": 50,"pid": 42,"title": "三级","money": "44444.00"},{"id": 52,"pid": 48,"title": "一级","money": "21234.00"},{"id": 57,"pid": 53,"title": "一级","money": "4332.00"},{"id": 61,"pid": 38,"title": "啊","money": "50.00"},{"id": 62,"pid": 1,"title": "1231","money": "1.00"},{"id": 63,"pid": 0,"title": "阿斯顿飞过","money": "0.00"},{"id": 66,"pid": 1,"title": "1","money": "23.00"},{"id": 67,"pid": 2,"title": "123","money": "123.00"},{"id": 70,"pid": 2,"title": "123","money": "123.00"},{"id": 71,"pid": 38,"title": "桀桀桀","money": "0.00"},{"id": 72,"pid": 12,"title": "11","money": "11.00"},{"id": 73,"pid": 12,"title": "11","money": "11.00"},{"id": 77,"pid": 1,"title": "测试一下","money": "10000000.00"},{"id": 78,"pid": 1,"title": "七龙珠1","money": "999.00"},{"id": 79,"pid": 2,"title": "测试","money": "100.00"},{"id": 80,"pid": 1,"title": "测试一下","money": "10000000.00"},{"id": 81,"pid": 0,"title": "我编辑了一下","money": "123123.00"},{"id": 94,"pid": 81,"title": "我添加了一下","money": "4567.00"},{"id": 97,"pid": 96,"title": "测试","money": "0.00"},{"id": 101,"pid": 100,"title": "神奇","money": "1.00"},{"id": 102,"pid": 101,"title": "神奇_yi","money": "1.00"},{"id": 103,"pid": 102,"title": "神奇_er","money": "1.00"},{"id": 104,"pid": 96,"title": "测试","money": "0.00"},{"id": 109,"pid": 105,"title": "测试","money": "1.00"},{"id": 111,"pid": 103,"title": "1","money": "1.00"},{"id": 112,"pid": 111,"title": "11","money": "11.00"},{"id": 113,"pid": 112,"title": "111","money": "111.00"},{"id": 114,"pid": 113,"title": "1112","money": "1112.00"},{"id": 115,"pid": 114,"title": "11122","money": "11122.00"},{"id": 116,"pid": 103,"title": "1","money": "1.00"},{"id": 117,"pid": 4,"title": "1","money": "1.00"},{"id": 118,"pid": 4,"title": "1","money": "1.00"},{"id": 119,"pid": 2,"title": "","money": "0.00"},{"id": 120,"pid": 2,"title": "","money": "0.00"},{"id": 121,"pid": 4,"title": "nihao","money": "789.00"},{"id": 122,"pid": 94,"title": "纳尼?","money": "1111.00"},{"id": 123,"pid": 122,"title": "测试1","money": "1111.00"},{"id": 124,"pid": 123,"title": "???","money": "1111.00"},{"id": 125,"pid": 124,"title": "嗯?","money": "1111.00"},{"id": 126,"pid": 125,"title": "怎么回事?","money": "1111.00"},{"id": 127,"pid": 94,"title": "纳尼?","money": "1111.00"},{"id": 128,"pid": 124,"title": "嗯?","money": "1111.00"},{"id": 129,"pid": 125,"title": "吆西","money": "123.00"},{"id": 130,"pid": 125,"title": "吆西","money": "123.00"},{"id": 131,"pid": 130,"title": "123","money": "123.00"},{"id": 132,"pid": 130,"title": "123","money": "123.00"}]
js逻辑部分

首先将拿到的数据进行处理

    //处理好的数据let res = getTree(temp,0,[]);console.log(res);//打印观察

先获取到所有的以及数据

    //处理数据方法function getTree(list,pid,data){//获取所有一级for(let item of list){if(item.pid == pid){data.push(item);}}return data;}

获取二级及三级数据

        //获取子级for(let i of data){i.children = [];getTree(list,i.id,i.children);//递归调用if(i.children.length == 0){delete i.children;}}

逻辑综合代码

    //处理好的数据let res = getTree(temp,0,[]);console.log(res);//打印观察//处理数据方法function getTree(list,pid,data){//获取所有一级for(let item of list){if(item.pid == pid){data.push(item);}}//获取子级for(let i of data){i.children = [];getTree(list,i.id,i.children);//递归调用if(i.children.length == 0){delete i.children;}}return data;}

效果如下:

044a51aa69ee4e99882bb29e7573e22b.png

本篇博客结束谢谢观看,如有更好的意见请在评论区发出来讨论。

3841e1bddc2c48a4916722101e7d7482.gif

http://www.lryc.cn/news/99747.html

相关文章:

  • vue指令-v-text和v-html
  • quartus工具篇——PLL IP核的使用
  • [Angular] Import TranslateModule in Angular 16
  • Web自动化测试高级定位xpath
  • 2023河南萌新联赛第(二)场:河南工业大学 F - 最短距离
  • 前端文件上传实践与后端处理——文件分块上传
  • SFP6012A-ASEMI代理海矽美快恢复二极管参数、尺寸、规格
  • githack的安装步骤+一次错误体验
  • 【Spring框架】SpringBoot创建和使用
  • 【C语言项目】多臂井径电子测井成像项目(一)
  • 力扣 56. 合并区间
  • 前端开发Vue3.0 标签setup语法『UI组件库』之『模态框』【业务提升必备】
  • 在CSDN学Golang云原生(Kubernetes二开)
  • chatglm-6b量化推理指标记录
  • Android kotlin系列讲解之最佳的UI体验 - Material Design 实战
  • 链表基础知识
  • process.env.npm_config_argv的值3个参数remain、cooked、original什么含义
  • 【飞书】飞书导出md文档 | 飞书markdown文档导出 | 解决飞书只能导出pdf word
  • 零信任网络架构与实现技术的研究与思考
  • Unity 性能优化二:内存问题
  • JavaScript与TypeScript的区别
  • 【NetCore】05-使用Autofac增强容器能力
  • sparksql参数
  • STM32读写内部Flash
  • golang文件锁,目录锁,syscall包的使用
  • 数据库数据恢复-Syabse数据库存储页底层数据杂乱的数据恢复案例
  • 移远通信推出新一代高算力智能模组SG885G-WF,为工业和消费级IoT应用带来全新性能标杆
  • 微信小程序开发,小程序类目符合,线上版本无权限申请wx.getLocation接口
  • vue2企业级项目(五)
  • 【HTML5】拖放详解及实现案例