当前位置:网站首页>一文带你吃透js处理树状结构数据的增删改查
一文带你吃透js处理树状结构数据的增删改查
2022-07-30 05:14:00 【m0_67391518】
目录
问题描述:JS处理树状结构的增删改查
最近在开发一个后台管理系统的权限管理模块,涉及到各种树状结构的数据处理逻辑,例如:增,删,改,查等;相比普通的数组结构数据,树状结构的处理就没有数组那么的直观,但是也没那么复杂,需要多一步——递归查找来对数据进行深度遍历操作,那么这里呢,博主也将开发过程中总结出来的方法分享给大家,一文带你吃透JS树装结构数据处理:

数据结构示例
let data = [{
id: 1,
label: '一级 1',
children: [{
id: 4,
label: '二级 1-1',
children: [{
id: 9,
label: '三级 1-1-1'
}, {
id: 10,
label: '三级 1-1-2'
}]
}]
}, {
id: 2,
label: '一级 2',
children: [{
id: 5,
label: '二级 2-1'
}, {
id: 6,
label: '二级 2-2'
}]
}, {
id: 3,
label: '一级 3',
children: [{
id: 7,
label: '二级 3-1'
}, {
id: 8,
label: '二级 3-2'
}]
}];
解决方案:
1、新增节点
查找树装结构的指定节点,新增子节点,代码如下:
const appendNodeInTree = (id, tree, obj) => {
tree.forEach(ele=> {
if (ele.id === id) {
ele.children ? ele.children.push(obj) : ele.children = [obj]
} else {
if (ele.children) {
appendNodeInTree(id, ele.children, obj)
}
}
})
return tree
}
2、删除节点
查找树装结构的指定节点,删除节点,代码如下
const removeNodeInTree=(treeList, id)=> { // 通过id从数组(树结构)中移除元素
if (!treeList || !treeList.length) {
return
}
for (let i = 0; i < treeList.length; i++) {
if (treeList[i].id === id) {
treeList.splice(i, 1);
break;
}
removeNodeInTree(treeList[i].children, id)
}
}
3、修改节点
递归查找并修改某个节点的状态,代码如下:
const updateNodeInTree=(treeList,id, obj)=> {
if (!treeList || !treeList.length) {
return;
}
for (let i = 0; i < treeList.length; i++) {
if (treeList[i].id == id) {
treeList[i]= obj;
break;
}
updateNodeInTree(treeList[i].children,id,obj);
}
}
4、查找节点
递归查找树形节点的某个节点,代码:
const findNodeInTree = (data, key, callback) => {
for (let i = 0; i < data.length; i++) {
if (data[i].key == key) {
return callback(data[i], i, data)
}
if (data[i].children) {
findNodeInTree (data[i].children, key, callback)
}
}
}
// 所查找到的节点要存储的方法
let Obj={}
findNodeInTree(data, key, (item, index, arr) => {
Obj = item
})
// 此时就是Obj对应的要查找的节点
console.log(Obj)
总结
本文介绍了js如何处理树装数据结构的增删改查,掌握以上函数,基本可以应对同样业务类型的数据处理,更多js处理数据问题方面的疑难杂症,推荐查看博主之前总结的文章,喜欢博主的朋友可以给博主点个关注,点关注,不迷路,博主带你上高速~~

边栏推荐
- 字符串问题(上)
- LeetCode Algorithm 328. Parity linked list
- Requirements design document and the changing role of the product manager
- 22. Why do you need a message queue?What are the advantages of using the message queue?
- Unity stepping on the pit record - the use of GetComponent
- 力扣541-反转字符串2——双指针法
- 涂鸦Wi-Fi&BLE SoC开发幻彩灯带
- mysql隔离级别
- el-table中加入el-input框和el-input-number框,实现el-table的可编辑功能
- 【Vitis】ZCU102开发板PS端控制PL端复位的代码实现
猜你喜欢

Us to raise interest rates by 75 basis points in "technical recession"?Encryption market is recovering

Some understanding of YOLOv7

无代码开发平台子管理员入门教程

mysql无法远程连接 Can‘t connect to MySQL server on ‘xxx.xxx.xxx.xxx‘ (10060 “Unknown error“)

开源之夏 2022 与您相约!

Whole process scheduling - Azkaban entry and advanced

GO language study notes one

Protobuf compound data types, speaking, reading and writing

1315_使用LOOPBACK模拟模式pyserial安装是否成功的测试

给小白的 PG 容器化部署教程(下)
随机推荐
pycharm上的tensorflow环境搭载
给小白的 PG 容器化部署教程(下)
SVN 查看用户名密码
小程序npm包--API Promise化
Dynamic Programming Problems (End)
Kyligence 亮相第五届南方信息大会并获评“CIO 优选数字化服务商”
pytorch官网中如何选择以及后面的安装和pycharm测试步骤
4. Web Development
RadonDB MySQL on K8s 2.1.4 发布!
[Vitis] Code implementation of ZCU102 development board PS-side control PL-side reset
BindingExpression path error: 'selectMenusList' property not found on 'object' ''ViewModel'
ThinkPHP高仿蓝奏云网盘系统源码/对接易支付系统程序
Intermediate - interview questions
Hexagon_V65_Programmers_Reference_Manual (11)
uni-app realizes cross-end development of mobile phone Bluetooth to receive and send data
WPF introduces ttf icon file usage record
Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)
Discourse 自定义头部链接(Custom Header Links)
解读 Kylin 3.0.0 | 更敏捷、更高效的 OLAP 引擎
Whole process scheduling - Azkaban entry and advanced