当前位置:网站首页>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【基础知识 01】核心概念+特点+Web界面+架构+Job类型(一篇即可入门Azkaban工作流调度系统)

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

Interview shock 68: why does TCP need three handshakes?

Matlab Simulink realizes fuzzy PID control of time-delay temperature control system of central air conditioning

Use of tabbarcontroller

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)

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

protobuf的基本用法

论文笔记(SESSION-BASED RECOMMENDATIONS WITHRECURRENT NEURAL NETWORKS)

Solve NPM -v sudden failure and no response
随机推荐
Logical architecture of MySQL
JS one line code to obtain the maximum and minimum values of the array
服务发现原理分析与源码解读
RMQ学习笔记
Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market
JS judge the data types object.prototype.tostring.call and typeof
spolicy请求案例
Redis sentinel mode setup under Windows
Use of tabbarcontroller
一种分布式深度学习编程新范式:Global Tensor
新增市场竞争激烈,中国移动被迫推出限制性超低价5G套餐
Qt随手笔记(三)在vs中使用QtCharts画折线图
【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛
Principle analysis and source code interpretation of service discovery
R语言ggpubr包ggsummarystats函数可视化分组箱图(自定义分组颜色)并在X轴标签下方添加分组对应的统计值(样本数N、中位数median、四分位数的间距iqr、统计值的色彩和分组图色匹配
JS判断数据类型 Object.prototype.toString.call和typeof
Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice
Node 内存溢出及V8垃圾回收机制
莫队学习总结(二)
IIS网站配置