当前位置:网站首页>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 !
边栏推荐
- 全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功
- 【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移
- Apache installation problem: configure: error: APR not found Please read the documentation
- Solve server returns invalid timezone Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
- 110. Network security penetration test - [privilege promotion 8] - [windows sqlserver xp_cmdshell stored procedure authorization]
- Is it safe to open an account in Ping An Securities mobile bank?
- Attack and defense world - PWN learning notes
- 解决 Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
- The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
- Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition
猜你喜欢

Up meta - Web3.0 world innovative meta universe financial agreement

Minimalist movie website

Completion report of communication software development and Application

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

EPP+DIS学习之路(2)——Blink!闪烁!

Configure an encrypted web server

Xiaohongshu microservice framework and governance and other cloud native business architecture evolution cases

什么是ESP/MSR 分区,如何建立ESP/MSR 分区

Attack and defense world - PWN learning notes

SQL Lab (41~45) (continuous update later)
随机推荐
数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题
GCC compilation error
2022年在启牛开华泰的账户安全吗?
SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
Static comprehensive experiment
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
Apache installation problem: configure: error: APR not found Please read the documentation
VSCode的学习使用
Tutorial on principles and applications of database system (010) -- exercises of conceptual model and data model
TypeScript 接口继承
Niuke website
idea 2021中文乱码
Review and arrangement of HCIA
What are the top-level domain names? How is it classified?
开发一个小程序商城需要多少钱?
RHSA first day operation
Is it safe to open Huatai's account in kainiu in 2022?
Inverted index of ES underlying principle
跨域问题解决方案
Attack and defense world - PWN learning notes