当前位置:网站首页>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 !
边栏推荐
- 盘点JS判断空对象的几大方法
- zero-shot, one-shot和few-shot
- [pytorch practice] image description -- let neural network read pictures and tell stories
- Epp+dis learning road (2) -- blink! twinkle!
- Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
- Zhimei creative website exercise
- About sqli lab less-15 using or instead of and parsing
- Using stack to convert binary to decimal
- 2022年在启牛开华泰的账户安全吗?
- NGUI-UILabel
猜你喜欢
数据库系统原理与应用教程(011)—— 关系数据库
(待会删)yyds,付费搞来的学术资源,请低调使用!
Solutions to cross domain problems
VSCode的学习使用
即刻报名|飞桨黑客马拉松第三期盛夏登场,等你挑战
百度数字人度晓晓在线回应网友喊话 应战上海高考英语作文
110. Network security penetration test - [privilege promotion 8] - [windows sqlserver xp_cmdshell stored procedure authorization]
【统计学习方法】学习笔记——支持向量机(上)
Solve server returns invalid timezone Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
Epp+dis learning road (2) -- blink! twinkle!
随机推荐
<No. 9> 1805. Number of different integers in the string (simple)
H3C HCl MPLS layer 2 dedicated line experiment
Several methods of checking JS to judge empty objects
Introduction and application of smoothstep in unity: optimization of dissolution effect
Vxlan 静态集中网关
The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
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
Apache installation problem: configure: error: APR not found Please read the documentation
【PyTorch实战】图像描述——让神经网络看图讲故事
Processing strategy of message queue message loss and repeated message sending
SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
What are the technical differences in source code anti disclosure
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
小红书微服务框架及治理等云原生业务架构演进案例
关于 Web Content-Security-Policy Directive 通过 meta 元素指定的一些测试用例
牛客网刷题网址
Attack and defense world ----- summary of web knowledge points
全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功
跨域问题解决方案
顶级域名有哪些?是如何分类的?