当前位置:网站首页>Convert flat structure to tree structure
Convert flat structure to tree structure
2022-06-29 02:04:00 【wam923259736】
let list = [
{
id: 1,
label: "1",
pid: 0,
},
{
id: 11,
pid: 1,
label: "1-1",
},
{
id: 111,
pid: 11,
label: "1-1-1",
},
{
id: 2,
label: "2",
pid: 0,
},
{
id: 22,
pid: 2,
label: "2-2",
},
];
function tranListToTreeData(list, rootValue) {
var arr = [];
list.forEach((item) => {
if (item.pid === rootValue) {
// After finding it I'm going to find item Is there any child node below
const children = tranListToTreeData(list, item.id);
if (children.length) {
// If children Is longer than 0 Indicates that the child node is found
item.children = children;
}
arr.push(item); // Add the contents to the array
}
});
return arr;
}
let result = tranListToTreeData(list, 0);
// Converted list The tree structure is
[{
id: 1,
pid: 0,
label: "1",
children: [{
id: 11,
pid: 1,
label: "1-1",
children: [{
id: 111,
pid: 11,
label: "1-1-1",
}]
}]
},{
id: 2,
pid: 0,
label: '2',
children: [{
id: 22,
pid: 2,
label: '2-2'
}]
}]
边栏推荐
- Blog publishing test 1
- Introduction to UE gameplay 44 (animation import FBX and production standard)
- SystemVerilog-结构体(一)
- Analysis of sending principle of OData metadata request for SAP ui5 application
- Written examination questions, answers and analysis of Digital IC design and FPGA design (2) 2021 Huawei Hisilicon (Part 1)
- 【内网穿透】Frp 自建跳板-两个内网通讯上线-反弹shell
- P7 Erkai early know - registration and application creation
- TiFlash 面向编译器的自动向量化加速
- How to prevent virus
- QT basics tutorial: qstringlist
猜你喜欢
![[redis] get to know redis for the first time](/img/02/3c6a7f6ea8c563386a4cd458024728.png)
[redis] get to know redis for the first time

PR FAQ: how to retrieve accidentally deleted video and audio in PR?

【Redis】初识 Redis

How to use PN junction to measure temperature?
![[image detection] recognition of the front and back of a coin based on texture features with matlab code attached](/img/61/1fb15e9defa1fc471c4d2d34cc1ed4.jpg)
[image detection] recognition of the front and back of a coin based on texture features with matlab code attached

Crawler exercise (IV) -- IP address problem

Research on VB multi-layer firewall technology - state detection

Stm32l4xx serial port log configuration analysis

【Redis】Set类型

The metadata request parsing principle of OData XML format applied by SAP ui5 is based on domparser
随机推荐
Three tips from small video app source code developers on small video platforms
[MySQL practice of high concurrency, high performance and high availability of massive data -9] - transaction concurrency control solutions lbcc and mvcc
Uniapp notes
QT basics tutorial: qstringlist
How to choose source code encryption software
如何成为一名高级数字 IC 设计工程师(4-5)脚本篇:Shell 脚本实现的文件比较操作
Junior final exam
Business system anti-virus
Crawler exercise (IV) -- IP address problem
Adding, deleting, checking and modifying stack - dynamic memory
Scala 基礎 (三):運算符和流程控制
如何成为一名高级数字 IC 设计工程师(3-5)工具篇:SpyGlass 技术
同花顺究竟是如何开户?现在网上开户安全么?
利用kubernetes資源鎖完成自己的HA應用
Who do you want to know when opening a stock account? Is it safe to open an account online now?
Why install an SSL certificate on a web site?
TiFlash 面向编译器的自动向量化加速
什么叫股票线上开户?网上开户安全么?
PR FAQ: how to retrieve accidentally deleted video and audio in PR?
如何成为一名高级数字 IC 设计工程师(4-3)脚本篇:C 语言实现的文件读写操作