当前位置:网站首页>This article will take you through js to deal with the addition, deletion, modification and inspection of tree structure data
This article will take you through js to deal with the addition, deletion, modification and inspection of tree structure data
2022-07-30 05:25:00 【m0_67391518】
Table of Contents
Problem description: JS handles tree structureCRUD
Problem description: JS handles addition, deletion, modification and checking of tree structures
Recently I am developing a permission management module of a background management system, which involves data processing logic of various tree structures, such as adding, deleting, changing, checking, etc. Compared with ordinary array structure data, tree structure dataThe processing is not as intuitive as the array, but it is not so complicated. It needs one more step-recursive search to perform a deep traversal operation on the data. Then here, the blogger will also share the methods summed up in the development process with everyone.You understand JS tree structure data processing:

Data structure example
let data = [{id: 1,label: 'Level 1',children: [{id: 4,label: 'Secondary 1-1',children: [{id: 9,label: 'Level 3 1-1-1'}, {id: 10,label: 'Level 3 1-1-2'}]}]}, {id: 2,label: 'Level 2',children: [{id: 5,label: 'Secondary 2-1'}, {id: 6,label: 'Secondary 2-2'}]}, {id: 3,label: 'Level 3',children: [{id: 7,label: 'Secondary 3-1'}, {id: 8,label: 'Secondary 3-2'}]}];Solution:
1. New node
Find the specified node of the tree structure and add a new child node, the code is as follows:
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, delete node
Find the specified node of the tree structure and delete the node, the code is as follows
const removeNodeInTree=(treeList, id)=> { // remove element from array (tree structure) by idif (!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. Modify node
Recursively find and modify the state of a node, the code is as follows:
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. Find node
Recursively find a node of the tree node, code:
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)}}}// The method to store the found nodelet Obj={}findNodeInTree(data, key, (item, index, arr) => {Obj = item})// This is the node to be found corresponding to Objconsole.log(Obj)Summary
This article introduces how js handles the addition, deletion, modification and query of tree-mounted data structures. Mastering the above functions can basically deal with data processing of the same business type. For more intricate problems in js processing data problems, it is recommended to view the blogger's previous summaryFriends who like bloggers can pay attention to bloggers, pay attention, don't get lost, bloggers will take you on the highway~~

边栏推荐
- Docker-compose安装mysql
- 为Bitbucket 和 Sourcetree 设置SSL认证
- 22. 为什么需要消息队列?使用消息队列有什么好处?
- ms project2010项目管理软件使用技巧总结
- Summary of skills in using ms project2010 project management software
- Hexagon_V65_Programmers_Reference_Manual (12)
- 即刻报名|前沿技术探索:如何让 Spark 更强劲、更灵活
- 行业案例|数字化经营底座助力寿险行业转型
- curl (7) Failed connect to localhost8080; Connection refused
- 关于组织开展2022年广东省技术先进型服务企业认定工作的通知
猜你喜欢

Hexagon_V65_Programmers_Reference_Manual (12)

mysql基础(4)

Small programs use npm packages to customize global styles

Acwing完全数

Golang go-redis cluster模式下不断创建新连接,效率下降问题解决

小程序使用npm包定制全局样式

pycharm上的tensorflow环境搭载

容器化 | 在 K8s 上部署 RadonDB MySQL Operator 和集群

程序员赚钱实操,手把手教你做付费课程,自媒体,付费文章及付费技术课赚钱

Six, read application configuration + log configuration
随机推荐
Docker-compose安装mysql
Code readability, pre-checks, comments and summaries
Whole process scheduling - Azkaban entry and advanced
The Double Pointer Problem (Part 2)
DLL说明(1)
Hexagon_V65_Programmers_Reference_Manual (12)
小程序使用npm包定制全局样式
L2-020 descendants of kung fu
(RCE) Remote Code/Command Execution Vulnerability Vulnerability Exercise
Record of problems encountered by the pyinstaller packager
容器化 | 构建 RadonDB MySQL 集群监控平台
[High Performance Computing] openMP
Acwing perfect number
Predictive maintenance scheduling of multiple power equipment based on data-driven fault prediction
聊一聊什么是SaaS,以及遇到的问题......
C language implements highly secure game archives and reads files
剑指offer(刷题篇12)
MySQL夺命10问,你能坚持到第几问?
容器化 | 在 KubeSphere 中部署 MySQL 集群
go language study notes 4