let arr = [

    {id:1,name:"php",pid:0},
{id:2,name:"php基础",pid:1},
{id:3,name:"php面向对象",pid:1},
{id:4,name:"python",pid:0},
{id:5,name:"python迭带器",pid:4},
{id:6,name:"python面向对象",pid:4},
{id:7,name:"python网络编程",pid:4},
]; // 查询一级分类
function findChild(arr,id) {
let childs = [];
arr.forEach(v=>{
if (v.pid == id){
childs.push(v);
}
});
return childs;
} //
function build_tree(id) {
let childs= findChild(arr,id);
if (childs.length==0){
return null;
} // 对于父节点为0的进行循环,然后查出父节点为上面结果id的节点内容
childs.forEach((v,k)=>{
let buildTree = build_tree(v.id);
if (null != buildTree){
v['children'] = buildTree;
}
}); return childs; } console.log(build_tree(0));

最新文章

  1. “error LNK2019: 无法解析的外部符号”之分析
  2. java中request,application,session三个域及参数简单示例
  3. Fish入门
  4. CS异步下载
  5. angularjs学习笔记三——directive
  6. Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (二) —— SQLite
  7. 【CSS】盒子模型 之 IE 与W3C的盒子模型对比
  8. python第一篇-------python介绍
  9. MySQL事务隔离级别的实现原理
  10. windows NLB实现MSSQL读写分离--从数据库集群读负载均衡
  11. 第二部分之Redis服务器(第十四章)
  12. Linux 驱动——Button驱动4(fasync)异步通知
  13. PHP常用函数(一):数组常用函数
  14. 【Git】Git中的冲突(图形界面,待更新...)
  15. linux的查找命令 find whereis locate
  16. redis hset hmset过期时间
  17. 20165321 实验三 敏捷开发与XP实践-4
  18. MySQL从删库到跑路_高级(七)——事务和锁
  19. Ansible Galaxy
  20. Android 混淆签名打包

热门文章

  1. Codeforces Round #433 (Div. 2)【A、B、C、D题】
  2. Java 中 Vector 和 ArrayList 的区别
  3. bzoj2336 [HNOI2011]任务调度
  4. php函数:call_user_func
  5. 几句代码简单实现IoC容器
  6. 【洛谷P2022】有趣的数
  7. 51Nod - 1205 (流水先调度)超级经典的贪心 模板题
  8. STM32F10X固件库函数——串口清状态位函数分析
  9. 基于VC++的WinCE网口通信
  10. 用 crontab 实现开机自动运行脚本