当前位置:网站首页>扁平结构转换为树结构
扁平结构转换为树结构
2022-06-29 01:57: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) {
// 找到之后 就要去找 item 下面有没有子节点
const children = tranListToTreeData(list, item.id);
if (children.length) {
// 如果children的长度大于0 说明找到了子节点
item.children = children;
}
arr.push(item); // 将内容加入到数组中
}
});
return arr;
}
let result = tranListToTreeData(list, 0);
// 转换后的list树结构为
[{
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'
}]
}]
边栏推荐
- When you complain about the roll, others have quietly begun to prepare for the golden three silver four
- Dialogue with opensea co creation Alex: we still only touch the tip of the iceberg of NFT capability | chain catcher
- How does flush open an account? Is it safe to open an account online?
- C语言课程设计------食品仓库管理系统
- Testing until you're 35? The 35 + test will lead to unemployment?
- [solution] longest common subsequence
- Scala 基础 (三):运算符和流程控制
- How to become a senior digital IC Design Engineer (3-5) tools: Spyglass Technology
- Adding, deleting, checking and modifying stack - dynamic memory
- 【Redis】List类型
猜你喜欢

Callback function of unity after importing resources

直播预告|SQL也能玩转工业级机器学习?MLOps meetup V3带你一探究竟!

Application of X6 in data stack index management

Crawler exercise (IV) -- IP address problem

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

【Redis】Key的层级结构

C language course design - food warehouse management system
![[high concurrency, high performance and high availability of massive data MySQL practice-10] - Implementation of mvcc in InnoDB](/img/dc/a30ccd9943e668aef8c874980a4975.jpg)
[high concurrency, high performance and high availability of massive data MySQL practice-10] - Implementation of mvcc in InnoDB

Near consensus mechanism

Typescript (4) interface
随机推荐
The metadata request parsing principle of OData XML format applied by SAP ui5 is based on domparser
Large scale visual relationship understanding
大三下期末考试
MySQL realizes data comparison between two tables by calculating intersection and difference sets
Query any field of any table in JPA to the util method of entity class dto
【Redis】数据介绍 & 通用命令 & String类型
数字 IC 设计、FPGA 设计秋招笔试题目、答案、解析(2)2021 华为海思(上)
AHA C language, C language programming introductory books and PPT (PDF version) download website
PHP hospital network reservation management system source code, hospital consultation reservation registration OA system (commercial or graduation design)
Qt基础教程:QStringList
[TS] type alias
Callback function of unity after importing resources
How to use PN junction to measure temperature?
Understand flex layout in an article
Noip2006-2018 improvement group preliminary test questions improvement procedure questions csp-s 2019 2020 preliminary test questions improvement procedure questions
Dialogue with opensea co creation Alex: we still only touch the tip of the iceberg of NFT capability | chain catcher
Scala 基礎 (三):運算符和流程控制
利用kubernetes資源鎖完成自己的HA應用
如何成为一名高级数字 IC 设计工程师(6-7)数字 IC 验证篇:DEBUG 技巧
Introduction to UE gameplay 44 (animation import FBX and production standard)