当前位置:网站首页>Transform between tree and array in JS (hide the children field if the child node of the tree is empty)
Transform between tree and array in JS (hide the children field if the child node of the tree is empty)
2022-07-26 09:53:00 【Zhou Xiaotiao】
Blogging is for taking notes
Preface
In the use of elmentUI
perhaps antUI
Isochronous cascader
When cascading selectors , Tree structure required , Sometimes it is necessary to convert trees and arrays , The transformation of tree and array here will be sub children There is no need for empty time , Avoid choosing white pages in time and space ( As shown in Fig. 1 )
One 、 Tree to array
treeToArray(list, newArr = []) {
list.forEach(item => {
const {
children} = item
if (children) {
delete item.children
if (children.length) {
newArr.push(item)
return this.treeToArray(children, newArr)
}
}
newArr.push(item)
})
return newArr
}
Two 、 Array to tree
listToTree(list, parentId = null) {
return list.filter(item => item.parentId === parentId).map(item => {
let children= this.listToTree(list, item.id)
if (children.length > 0) {
return {
...item,
children
}
} else {
return {
...item
}
}
})
}
3、 ... and 、 Use
1.data Data defined in ( Trees )options
options: [
{
value: 'zhinan',
label: ' guide ',
parentId:'0',
id: "aa",
children: [
{
value: 'shejiyuanze',
label: ' Design principles ',
parentId:'aa',
id: "aa01",
children: [
{
value: 'yizhi',
label: ' Agreement ',
parentId:'aa01',
id: "aa0101",
},
{
value: 'fankui',
label: ' feedback ',
parentId:'aa01',
id: "aa0102",
},
]
},
]
},
{
value: 'zujian',
label: ' Components ',
parentId:'0',
id: "bb",
children: [
{
value: 'basic',
label: 'Basic',
parentId:'bb',
id: "bb01",
children: [
{
value: 'layout',
label: 'Layout Layout ',
parentId:'bb01',
id: "bb0101",
},
]
},
{
value: 'form',
label: 'Form',
parentId:'bb',
id: "bb02",
children: []
},
]
},
],
2. Use tree to array ( Note that it is deleted by default when converting to an array children
attribute )
let list = this.treeToArray(this.options)
console.log("list",list)
2. Use array to turn the tree
let tree = this.listToTree(list,list[0].parentId)
console.log("tree",tree)
Notice here when children
by []
when , Not mounted by default children
attribute
边栏推荐
- Mqtt x cli officially released: powerful and easy-to-use mqtt 5.0 command line tool
- 开发转测试:从0开始的6年自动化之路...
- 服务发现原理分析与源码解读
- EOJ 2020 1月月赛 E数的变换
- IIS website configuration
- Azkaban [basic knowledge 01] core concepts + features +web interface + Architecture +job type (you can get started with Azkaban workflow scheduling system in one article)
- [information system project manager] summary of essence of high-level series for the first time
- 2021 windows penetration of "Cyberspace Security" B module of Shandong secondary vocational group (analysis)
- Gauss elimination for solving XOR linear equations
- Matlab Simulink realizes fuzzy PID control of time-delay temperature control system of central air conditioning
猜你喜欢
Gauss elimination
Leetcode 504. 七进制数
protobuf的基本用法
Node memory overflow and V8 garbage collection mechanism
高斯消元
2019 ICPC Asia Yinchuan Regional(水题题解)
[fluorescent character effect]
SSG framework Gatsby accesses the database and displays it on the page
分布式网络通信框架:本地服务怎么发布成RPC服务
R语言ggplot2可视化: 将图例标题(legend title)对齐到ggplot2中图例框的中间(默认左对齐、align legend title to middle of legend)
随机推荐
2022 zhongkepan cloud - server internal information acquisition and analysis flag
Spolicy request case
R language ggplot2 visualization: align the legend title to the middle of the legend box in ggplot2 (default left alignment, align legend title to middle of legend)
服务发现原理分析与源码解读
Show default image when wechat applet image cannot be displayed
2019 ICPC Asia Yinchuan Regional(水题题解)
2021 windows penetration of "Cyberspace Security" B module of Shandong secondary vocational group (analysis)
Cloud native (36) | introduction and installation of harbor in kubernetes
Mysql5.7.25 master-slave replication (one-way)
JS table auto cycle scrolling, mouse move in pause
Interpretation of the standard of software programming level examination for teenagers_ second level
Xiaobai makes a wave of deep copy and shallow copy
PMM (percona monitoring and management) installation record
Installation and use of cocoapods
Keeping alive to realize MySQL automatic failover
服务器、客户端双认证
高斯消元求解矩阵的逆(gauss)
EOJ 2020 1月月赛 E数的变换
copyTo
JS one line code to obtain the maximum and minimum values of the array