当前位置:网站首页>一文带你吃透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处理数据问题方面的疑难杂症,推荐查看博主之前总结的文章,喜欢博主的朋友可以给博主点个关注,点关注,不迷路,博主带你上高速~~

边栏推荐
- 翻译 | 解读首部 Kubernetes 纪录片
- Hexagon_V65_Programmers_Reference_Manual (12)
- Small program npm package--API Promise
- DLL description (1)
- CSDN Meetup 回顾 丨从数据湖到指标中台,提升数据分析 ROI
- VisualStudio2022 local debugging entry is particularly slow problem solving
- Record of problems encountered by the pyinstaller packager
- Discourse Custom Header Links
- Acwing完全数
- pycharm上的tensorflow环境搭载
猜你喜欢

Six, read application configuration + log configuration

Hexagon_V65_Programmers_Reference_Manual(12)

IGBT wafers used in photovoltaic inverters

Unity stepping on the pit record - the use of GetComponent

WPF study notes "WPF Layout Basics"

mysql无法远程连接 Can‘t connect to MySQL server on ‘xxx.xxx.xxx.xxx‘ (10060 “Unknown error“)
![[High Performance Computing] openMP](/img/a5/2cfd760a26edb379d337eb3d1605d5.jpg)
[High Performance Computing] openMP

给小白的 PostgreSQL 容器化部署教程(上)

mysql隔离级别

Plan for many situations in the processing chain
随机推荐
5. View parsing and template engine
QT(39)-vs development qt program prompts that the source file cannot be opened
光明区关于促进科技创新的若干措施(征求意见稿)
ThinkPHP高仿蓝奏云网盘系统源码/对接易支付系统程序
22. Why do you need a message queue?What are the advantages of using the message queue?
暴力递归到动态规划 05 (贴纸拼词)
工作效率-十五分钟让你快速学习Markdown语法到精通排版实践备忘
Discourse 自定义头部链接(Custom Header Links)
Simulation problem (middle)
el-table中加入el-input框和el-input-number框,实现el-table的可编辑功能
Hexagon_V65_Programmers_Reference_Manual(10)
聊一聊什么是SaaS,以及遇到的问题......
ms project2010项目管理软件使用技巧总结
Dynamically bind href url
小程序npm包--API Promise化
容器化 | 在 KubeSphere 中部署 MySQL 集群
Requirements design document and the changing role of the product manager
mysql隔离级别
22. 为什么需要消息队列?使用消息队列有什么好处?
RadonDB PostgreSQL on K8s 2.1.0 发布!