当前位置:网站首页>扁平结构转换为树结构
扁平结构转换为树结构
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'
}]
}]
边栏推荐
- Would like to ask how to choose a securities firm? Is it safe to open an account online now?
- 【Redis】List类型
- [机缘参悟-33]:眼见不一定为实,大多数时候“眼见为虚”
- Noip2006-2018 improvement group preliminary test questions improvement procedure questions csp-s 2019 2020 preliminary test questions improvement procedure questions
- 什么叫股票线上开户?网上开户安全么?
- How to become a senior digital IC Design Engineer (3-5) tools: Spyglass Technology
- How to manage device authorization
- Rasa dialogue robot helpdesk (V)
- How to become a senior digital IC Design Engineer (6-5) digital IC Verification: coverage collection
- Pat grade a real problem 1165
猜你喜欢

【Redis】List类型

The left toolbar of hbuilder is missing

Redis data migration (III)

Interviewer: with the for loop, why do you need foreach??

分享自己平时使用的socket多客户端通信的代码技术点和软件使用

Research on VB multi-layer firewall technology - state detection

Uniapp notes

Oculusrifts and unity UI interaction (1) - Overview

ASP. Net based on LAN

SAP ui5 beginner tutorial 22 - development and use of filter
随机推荐
【Redis】Key的层级结构
栈的增删查改---动态内存
[C language] Fibonacci sequence and frog jumping steps (the most detailed elementary frog jumping steps)
Niuke.com Huawei question bank (41~50)
Scala Foundation (3): Operators and Process Control
How to become a senior digital IC Design Engineer (6-6) digital IC Verification: system level simulation
Oculusrifts and unity UI interaction (1) - Overview
Stm32l4xx serial port log configuration analysis
Teach you how to understand the test environment project deployment
Analysis of parsing principle of OData metadata request response in SAP ui5 application
[image detection] recognition of the front and back of a coin based on texture features with matlab code attached
How to become a senior digital IC Design Engineer (3-5) tools: Spyglass Technology
Business system anti-virus
Typescript (4) interface
Query any field of any table in JPA to the util method of entity class dto
Interviewer: with the for loop, why do you need foreach??
Basic use of flask Sqlalchemy
Live broadcast preview | can SQL also play industrial machine learning? Mlops meetup V3 takes you to the bottom!
How to become a senior digital IC Design Engineer (6-4) digital IC Verification: test point decomposition
How to become a senior digital IC Design Engineer (4-2) script: file read / write operation realized by Verilog HDL code