当前位置:网站首页>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 !
边栏推荐
- SQL lab 1~10 summary (subsequent continuous update)
- Using stack to convert binary to decimal
- 111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
- Is it safe to open Huatai's account in kainiu in 2022?
- Completion report of communication software development and Application
- 编译 libssl 报错
- The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
- In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
- How much does it cost to develop a small program mall?
- Up meta - Web3.0 world innovative meta universe financial agreement
猜你喜欢
VSCode的学习使用
金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
问题:先后键入字符串和字符,结果发生冲突
数据库系统原理与应用教程(007)—— 数据库相关概念
【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
"Series after reading" my God! It's so simple to understand throttling and anti shake~
Tutorial on principles and applications of database system (009) -- conceptual model and data model
【统计学习方法】学习笔记——提升方法
Inverted index of ES underlying principle
<No. 8> 1816. Truncate sentences (simple)
随机推荐
关于 Web Content-Security-Policy Directive 通过 meta 元素指定的一些测试用例
Learning and using vscode
NGUI-UILabel
(待会删)yyds,付费搞来的学术资源,请低调使用!
zero-shot, one-shot和few-shot
NGUI-UILabel
Introduction and application of smoothstep in unity: optimization of dissolution effect
《通信软件开发与应用》课程结业报告
静态Vxlan 配置
H3C HCl MPLS layer 2 dedicated line experiment
密码学系列之:在线证书状态协议OCSP详解
AirServer自动接收多画面投屏或者跨设备投屏
"Series after reading" my God! It's so simple to understand throttling and anti shake~
When OSPF specifies that the connection type is P2P, it enables devices on both ends that are not in the same subnet to Ping each other
Sonar:cognitive complexity
如何理解服装产业链及供应链
Epp+dis learning road (2) -- blink! twinkle!
idm服务器响应显示您没有权限下载解决教程
【统计学习方法】学习笔记——第四章:朴素贝叶斯法
About sqli lab less-15 using or instead of and parsing