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

3.递归求值

let treeList = [{id: '1',name: '父一',children: [{id: '1-1',name: '子一',children: [{ id: '1-1-1', name: '孙一一', children: null },{ id: '1-1-2', name: '孙一二', children: null },{ id: '1-1-3', name: '孙一三', children: null }]},]},{id: '2',name: '父二',children: [{ id: '2-1', name: '子二一', children: null },{ id: '2-2', name: '子二一', children: null },{ id: '2-3', name: '子二一', children: null }]},{id: '3',name: '父三',children: null}
]function find(treeList,code) {for(let i in treeList){//循环遍历,当id相等的时候  返回他的nameif (treeList[i].id == code) {return treeList[i].name}//如果不满足上面的条件,且还有children属性,执行下面代码if(treeList[i].children){//递归赋值let node = find(treeList[i].children,code)console.log(node + '这是node');//当node有值得时候,把子节点 父节点 关联的全部输出if (node !== undefined) {return node.concat(treeList[i].name)}}}
}
console.log(find(treeList,'1-1-1'));

http://www.mrgr.cn/news/33582.html

相关文章:

  • AI 智能名片链动 2+1 模式商城小程序中的体验策略
  • 如何访问字符串中某个字符
  • Redis 中 String 字符串类型详解
  • 【线程】线程的同步
  • 蓝桥杯1.小蓝的漆房
  • C++高精度计时方法总结(测试函数运行时间)
  • 240922-chromadb的基本使用
  • 以小人之心度君子之腹
  • C++之模板初阶
  • 简单题101. 对称二叉树 (python)20240922
  • 教你用 python 在国内实现 openAi 的调用
  • pod介绍与配置
  • 使用Properties
  • Spring Service中的@Service注解的使用
  • 英伟达开源 NVLM 1.0 引领多模态 AI 变革
  • Matlab R2018a怎么下载安装?Matlab R2018a保姆级详细安装教程
  • 普通程序员如何快速入门AIGC
  • Fyne ( go跨平台GUI )中文文档- 架构 (八)完结
  • golang学习笔记7-指针、值类型与引用类型
  • 滚雪球学SpringCloud[7.3讲]:分布式事务管理详解