当前位置:网站首页>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'
}]
}]
边栏推荐
- [redis] key hierarchy
- Tiflash compiler oriented automatic vectorization acceleration
- How to choose source code encryption software
- [MySQL practice of high concurrency, high performance and high availability of massive data -9] - transaction concurrency control solutions lbcc and mvcc
- Research on VB multi-layer firewall technology - state detection
- Query any field of any table in JPA to the util method of entity class dto
- 【学习笔记】子集和问题
- Callback function of unity after importing resources
- When you complain about the roll, others have quietly begun to prepare for the golden three silver four
- RISC CPU design based on FPGA (4) 36 questions about the project and their answers
猜你喜欢

【内网穿透】Frp 自建跳板-两个内网通讯上线-反弹shell

How to become a senior digital IC Design Engineer (4-3)

Redis data migration (III)

HBuilder左侧工具栏不见了

栈的增删查改---动态内存
![[redis] set type](/img/97/0a83016e89316849c01072044f2124.png)
[redis] set type

Callback function of unity after importing resources
![[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

SAP ui5 beginner tutorial 22 - development and use of filter

Exclusive analysis | real situation of software test about resume and interview
随机推荐
Redis data migration (III)
Analysis of parsing principle of OData metadata request response in SAP ui5 application
Test a CSDN free download software
Analysis of sending principle of OData metadata request for SAP ui5 application
How to become a senior digital IC Design Engineer (3-5) tools: Spyglass Technology
I'd like to ask you, where can I open an account in Zhongshan? Is it safe to open an account online?
A full screen gesture adaptation scheme
Ambiguity between 0 and 1
Near's storage stacking
The left toolbar of hbuilder is missing
网上联系客户经理办理炒股开户安全吗?
同花顺究竟是如何开户?现在网上开户安全么?
How to prevent virus
Tiflash compiler oriented automatic vectorization acceleration
How to become a senior digital IC Design Engineer (6-7) digital IC Verification: debug skills
Query any field of any table in JPA to the util method of entity class dto
如何成为一名高级数字 IC 设计工程师(4-5)脚本篇:Shell 脚本实现的文件比较操作
利用kubernetes資源鎖完成自己的HA應用
在按钮禁用时消除hover效果
[redis] key hierarchy