当前位置:网站首页>JS to convert array to tree data
JS to convert array to tree data
2022-07-07 12:29:00 【Xiaoding Chong duck!】
Subject requirements :
Convert the following array into a tree structure :
const nodes = [
{ id: 3, name: ' node C', parentId: 1 },
{ id: 6, name: ' node F', parentId: 3 },
{ id: 0, name: 'root', parentId: null },
{ id: 1, name: ' node A', parentId: 0 },
{ id: 8, name: ' node H', parentId: 4 },
{ id: 4, name: ' node D', parentId: 1 },
{ id: 2, name: ' node B', parentId: 0 },
{ id: 5, name: ' node E', parentId: 2 },
{ id: 7, name: ' node G', parentId: 2 },
{ id: 9, name: ' node I', parentId: 5 }
];The tree structure is :

Ideas :
1、 With id by key, Building an object obj, Used to find parent
2、 Iterate over the original array , adopt parentId from obj Parent node found in , Then insert yourself into the parent node children in
notes : Pay attention here , Object is a reference type , therefore map The object inside changes , The same node object in the corresponding original array will also change .
Code :
function convert(data) {
let result;
let map = {};
data.forEach(item => {
map[item.id] = item;
});
// console.log(map);
data.forEach(item => {
// item.pid by 0 when return underfined
let parent = map[item.parentId];
if (parent) {
(parent.children || (parent.children = [])).push(item);
} else {
// here push Of item yes pid by 0 The data of
result = item;
}
});
return result;
}Miao !
边栏推荐
- How to understand the clothing industry chain and supply chain
- Learning and using vscode
- 30. Few-shot Named Entity Recognition with Self-describing Networks 阅读笔记
- SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
- 2022年在启牛开华泰的账户安全吗?
- Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition
- What is a LAN domain name? How to parse?
- 编译 libssl 报错
- EPP+DIS学习之路(1)——Hello world!
- Sort out the garbage collection of JVM, and don't involve high-quality things such as performance tuning for the time being
猜你喜欢

问题:先后键入字符串和字符,结果发生冲突

《通信软件开发与应用》课程结业报告

How to use PS link layer and shortcut keys, and how to do PS layer link

Introduction and application of smoothstep in unity: optimization of dissolution effect

Review and arrangement of HCIA

【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移

SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)

Completion report of communication software development and Application

Airserver automatically receives multi screen projection or cross device projection

Solutions to cross domain problems
随机推荐
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
【PyTorch实战】用RNN写诗
111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
Visual studio 2019 (localdb) \mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports version 782 and earlier
AirServer自动接收多画面投屏或者跨设备投屏
NGUI-UILabel
Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios
Customize the web service configuration file
File upload vulnerability - upload labs (1~2)
【统计学习方法】学习笔记——第五章:决策树
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
Solutions to cross domain problems
利用棧來實現二進制轉化為十進制
Cenos openssh upgrade to version 8.4
sql-lab (54-65)
(待会删)yyds,付费搞来的学术资源,请低调使用!
How much does it cost to develop a small program mall?
千人规模互联网公司研发效能成功之路
SQL Lab (41~45) (continuous update later)
Zhimei creative website exercise