当前位置:网站首页>Flat data to tree
Flat data to tree
2022-07-28 17:29:00 【Joshua02】
Source data :
let arr = [
{id: 1, name: ' department 1', pid: 0},
{id: 2, name: ' department 2', pid: 1},
{id: 3, name: ' department 3', pid: 1},
{id: 4, name: ' department 4', pid: 3},
{id: 5, name: ' department 5', pid: 4},
];Need output :
[
{
"id": 1,
"name": " department 1",
"pid": 0,
"children": [
{
"id": 2,
"name": " department 2",
"pid": 1,
"children": [
{...}
]
},
{
"id": 3,
"name": " department 3",
"pid": 1,
"children": [
{...}
]
}
]
}
]
The easiest way , Cycle after sorting (filter It can also cycle )
function ToTree(arr){
return arr
.sort((a, b) => b.pid - a.pid)
.filter(item=>{
item.children = arr.filter(subitem=> subitem.pid === item.id);
return item.pid ===0;
})
}In terms of performance , Data can be converted to Map
function arrayToTree(arr) {
const result = []; // Store results
const mapData = {}; //
for (let item of arr) {
const id = item.id;
const pid = item.pid;
if (!mapData[id]) {
mapData[id] = {
children: [],
}
}
mapData[id] = {
...item,
children: mapData[id]['children']
}
const treeItem = mapData[id];
if (pid === 0) {
result.push(treeItem);
} else {
if (!mapData[pid]) {
mapData[pid] = {
children: [],
}
}
mapData[pid].children.push(treeItem)
}
}
return result;
}
边栏推荐
- Visual Studio 2012/2015发布Web应用连同.cs源码一起发布
- 2022 Niuke multi school second CDE
- Leetcode 2022.04.10 China Merchants Bank special competition D. store promotion (DP)
- Mysql database addition, deletion, modification and query (detailed explanation of basic operation commands)
- Differences between CNSA and CASC and CASIC
- Selection of resistance in high speed circuit
- 【sqoop】sqoop1.4.7 安装集成CDH5.13
- Gray code and binary conversion and typical examples (4bits gray code counter)
- Use of influxdb2
- Why do I choose to use go language?
猜你喜欢

Gray code and binary conversion and typical examples (4bits gray code counter)

Verilog 每日一题(VL14 自动贩售机1--FSM常见题型)

Firewalld防护墙

QR code generation of wechat applet with parameters

Goweb开发之Beego框架实战:第一节 Beego框架介绍

DGL Chapter 1 (official tutorial) personal notes

Zero foundation uses unity3d to develop AR applications and download 3D models remotely

利用SQL Server代理作业对数据库进行定时还原

微服务架构-服务注册中心和服务网关(6.8) (转载)

Shell脚本之免交互操作
随机推荐
Goweb开发之Beego框架实战:第一节 Beego框架介绍
Create a custom paging control
Shell脚本之AWK
@RequestParam使用
连接设计与测试平台——SystemVerilog 接口知识点总结
Verilog 每日一题(VL2 异步复位的串联T触发器--牛客网)
Goweb开发之Beego框架实战:第三节 程序执行流程分析
Soft exam review summary
The practice of beego framework developed by goweb: Section 4 database configuration and connection
微服务架构-服务注册中心和服务网关(6.8) (转载)
【impala】【报错解决】 Impala cannot read or execute the parent directory of dfs.domain.socket.path的解决方法
MySQL PgSQL realizes the merging of multiple lines of records into one line, grouping and merging, and dividing with specified characters
Why do I choose to use go language?
Self study examination in April 2021
Verilog 每日一题 (VL28 加减计数器)
Verilog 每日一题(VL29 单端口RAM)
线性代数及矩阵论(十)
MySQL detailed learning tutorial (recommended Collection)
火了 2 年的服务网格究竟给微服务带来了什么?(转载)
Using SQL server agent job to restore the database regularly