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