当前位置:网站首页>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
边栏推荐
- Azkaban [basic knowledge 01] core concepts + features +web interface + Architecture +job type (you can get started with Azkaban workflow scheduling system in one article)
- asp. Net using redis cache (2)
- Why does new public chain Aptos meet market expectations?
- Nodejs service background execution (forever)
- 2019 ICPC Asia Yinchuan Regional(水题题解)
- IE7 set overflow attribute failure solution
- 服务器环境配置全过程
- Use of selectors
- 服务器内存故障预测居然可以这样做!
- JS table auto cycle scrolling, mouse move in pause
猜你喜欢

面试突击68:为什么 TCP 需要 3 次握手?

苹果独占鳌头,三星大举复兴,国产手机在高端市场颗粒无收

Unstoppable, pure domestic PCs have been in place, and the monopoly of the U.S. software and hardware system has been officially broken

Qt随手笔记(三)在vs中使用QtCharts画折线图

解决npm -v突然失效 无反应

2021年山东省中职组“网络空间安全”B模块windows渗透(解析)

2019 ICPC Asia Yinchuan regional (water problem solution)

【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛

小白搞一波深拷贝 浅拷贝

MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具
随机推荐
Fiddler packet capturing tool for mobile packet capturing
(一)面扫描仪与机械臂的手眼标定(眼在手上)
Flutter Event 派发
B站这个视频我是跪着看完的
WARNING: [pool www] server reached pm. max_ children setting (5), consider raising it
Sqoop [environment setup 01] CentOS Linux release 7.5 installation configuration sqoop-1.4.7 resolve warnings and verify (attach sqoop 1 + sqoop 2 Latest installation package +mysql driver package res
The use of MySQL in nodejs
QT handy notes (VI) -- update interface, screenshot, file dialog box
JS judge the data types object.prototype.tostring.call and typeof
I finished watching this video on my knees at station B
R语言ggpubr包ggsummarystats函数可视化分组箱图(自定义分组颜色)并在X轴标签下方添加分组对应的统计值(样本数N、中位数median、四分位数的间距iqr、统计值的色彩和分组图色匹配
服务器、客户端双认证(2)
Flutter event distribution
2022 zhongkepan cloud - server internal information acquisition and analysis flag
服务器内存故障预测居然可以这样做!
Principle analysis and source code interpretation of service discovery
阿里云技术专家郝晨栋:云上可观测能力——问题的发现与定位实践
Interview shock 68: why does TCP need three handshakes?
Registration module use case writing
解决npm -v突然失效 无反应