javascript树形结构化

 前言

e9fa53884e25902f85443b0ca2dfbc8b.png

  我是歌谣 最好的种树是十年前 其次是现在 今天继续给大家带来的是javascript树形结构化的讲解

 环境配置

f0b81bb2a55a4c6dcc09f8dbd6726887.png

npm init -y
yarn add vite -D

修改page.json配置端口

d96850ec8d4ef0058f1e00af06c86367.png

{
  "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"
  }
}

 数据部分

1475d0b1db17a04251e43b67903b7387.png

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:"康康管理"
}
]

 处理逻辑部分

c97b984a78d395dc7f4f7fd871273e33.png

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)

 运行结果

89fcbc545463059b15071fadc4e2afd9.png

[
    {
        "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

6521767e7785cb7b59eb5e44ae0e481c.png

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
    })
}

 运行结果

29a91c1e9ed5259ec8c2476aa4229c30.png

[
    {
        "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": "康康管理"
                    }
                ]
            }
        ]
    }
]

总结

505f578bf4a1dfb66661d79819f9af9f.png

我是歌谣 想加入前端巅峰人才交流群私信我

9f53d07dbe7ac156cad7eb651b93d7e0.png

b383ec6652819392c24271b0e711f212.png

点击上方 蓝字 关注我们

点个在看你最好看

7335897d1a36bd6eff728850f48bad1c.png

下方查看历史文章

5da0196b196586d44fb0a2aead434373.png

原生+TS实现todolist效果

Vue3+typesctipt实现todolist效果

关于面向对象

关于this指向

posted @ 2023-10-17 07:00  前端导师歌谣  阅读(23)  评论(0)    收藏  举报  来源