前言
我是歌谣 最好的种树是十年前 其次是现在 今天继续给大家带来的是javascript树形结构化的讲解
环境配置
| npm init -y |
| yarn add vite -D |
修改page.json配置端口
| { |
| "name": "react_ts", |
| "version": "1.0.0", |
| "description": "", |
| "main": "index.js", |
| "scripts": { |
| "dev": "vite --port 3002", |
| "server":"ts-node-dev ./server/app.ts" |
| }, |
| "keywords": [], |
| "author": "", |
| "license": "ISC", |
| "devDependencies": { |
| "@types/express": "^4.17.17", |
| "@types/jquery": "^3.5.18", |
| "express": "^4.18.2", |
| "jquery": "^3.7.1", |
| "ts-node-dev": "^2.0.0", |
| "typescript": "^5.2.2", |
| "vite": "^4.4.9" |
| } |
| } |
| |
数据部分
| const data=[{ |
| id:2, |
| pid:0, |
| path:'/course', |
| name:"Course", |
| title:"课程管理" |
| },{ |
| id:3, |
| pid:2, |
| path:'/course', |
| name:"Course", |
| title:"歌谣管理" |
| },{ |
| id:4, |
| pid:3, |
| path:'/course', |
| name:"Course", |
| title:"方方管理" |
| }, |
| { |
| id:4, |
| pid:2, |
| path:'/course', |
| name:"Course", |
| title:"康康管理" |
| }, |
| { |
| id:5, |
| pid:4, |
| path:'/course', |
| name:"Course", |
| title:"康康管理" |
| } |
| ] |
处理逻辑部分
| function formatDataTree(data){ |
| let parents=data.filter(p=>p.pid===0), |
| children=data.filter(c=>c.pid!==0) |
| |
| dataToTree(parents,children) |
| console.log(parents) |
| function dataToTree(parents,children){ |
| parents.map(p=>{ |
| children.map((c,i)=>{ |
| if(c.pid===p.id){ |
| console.log("--------") |
| let _children=JSON.parse(JSON.stringify(children)) |
| _children.splice(i,1) |
| console.log(_children,"_children is") |
| dataToTree([c],_children) |
| if(p.children){ |
| p.children.push(c) |
| }else{ |
| p.children=[c] |
| } |
| } |
| } |
| |
| ) |
| }) |
| } |
| } |
| |
| formatDataTree(data) |
运行结果
[
{
“id”: 3,
“pid”: 2,
“path”: “/course”,
“name”: “Course”,
“title”: “歌谣管理”,
“children”: [
{
“id”: 4,
“pid”: 3,
“path”: “/course”,
“name”: “Course”,
“title”: “方方管理”,
“children”: [
{
“id”: 5,
“pid”: 4,
“path”: “/course”,
“name”: “Course”,
“title”: “康康管理”
}
]
}
]
},
{
“id”: 4,
“pid”: 3,
“path”: “/course”,
“name”: “Course”,
“title”: “方方管理”
}
]
处理逻辑2
| function formatDataTree(data){ |
| let _data=JSON.parse(JSON.stringify(data)) |
| return _data.filter(p=>{ |
| const _arr=_data.filter(c=>c.pid===p.id); |
| _arr.length&&(p.children=_arr) |
| return p.pid===0 |
| }) |
| } |
运行结果
| [ |
| { |
| "id": 2, |
| "pid": 0, |
| "path": "/course", |
| "name": "Course", |
| "title": "课程管理", |
| "children": [ |
| { |
| "id": 3, |
| "pid": 2, |
| "path": "/course", |
| "name": "Course", |
| "title": "歌谣管理", |
| "children": [ |
| { |
| "id": 4, |
| "pid": 3, |
| "path": "/course", |
| "name": "Course", |
| "title": "方方管理", |
| "children": [ |
| { |
| "id": 5, |
| "pid": 4, |
| "path": "/course", |
| "name": "Course", |
| "title": "康康管理" |
| } |
| ] |
| } |
| ] |
| }, |
| { |
| "id": 4, |
| "pid": 2, |
| "path": "/course", |
| "name": "Course", |
| "title": "康康管理", |
| "children": [ |
| { |
| "id": 5, |
| "pid": 4, |
| "path": "/course", |
| "name": "Course", |
| "title": "康康管理" |
| } |
| ] |
| } |
| ] |
| } |
| ] |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
2022-10-07 jira项目笔记23-ts中 as const 使用
2022-10-07 jira项目笔记21-ant design table有tableProps属性
2022-10-07 学习笔记jira项目4-对比常见mock方案
2022-10-07 学习笔记jira项目3-解决一些问题
2022-10-07 学习笔记jira项目2-初始化项目
2022-10-07 jira项目笔记20-useSearchParams
2022-10-07 jira项目笔记19-as 关键字用于断言