当前位置:网站首页>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 !
边栏推荐
- idea 2021中文乱码
- 小红书微服务框架及治理等云原生业务架构演进案例
- 密码学系列之:在线证书状态协议OCSP详解
- Review and arrangement of HCIA
- Using stack to convert binary to decimal
- Sign up now | oar hacker marathon phase III midsummer debut, waiting for you to challenge
- 30. Feed shot named entity recognition with self describing networks reading notes
- Unity中SmoothStep介绍和应用: 溶解特效优化
- RHSA first day operation
- 【统计学习方法】学习笔记——支持向量机(上)
猜你喜欢

The IDM server response shows that you do not have permission to download the solution tutorial

Experiment with a web server that configures its own content

Unity 贴图自动匹配材质工具 贴图自动添加到材质球工具 材质球匹配贴图工具 Substance Painter制作的贴图自动匹配材质球工具

解密GD32 MCU产品家族,开发板该怎么选?

关于 Web Content-Security-Policy Directive 通过 meta 元素指定的一些测试用例

数据库系统原理与应用教程(009)—— 概念模型与数据模型

Unity map auto match material tool map auto add to shader tool shader match map tool map made by substance painter auto match shader tool

Tutorial on principles and applications of database system (010) -- exercises of conceptual model and data model

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

Several methods of checking JS to judge empty objects
随机推荐
@Bean与@Component用在同一个类上,会怎么样?
[pytorch practice] image description -- let neural network read pictures and tell stories
《看完就懂系列》天哪!搞懂节流与防抖竟简单如斯~
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
ps链接图层的使用方法和快捷键,ps图层链接怎么做的
OSPF exercise Report
SQL Lab (46~53) (continuous update later) order by injection
ES底层原理之倒排索引
Idea 2021 Chinese garbled code
An error occurred when vscade tried to create a file in the target directory: access denied [resolved]
【统计学习方法】学习笔记——第四章:朴素贝叶斯法
平安证券手机行开户安全吗?
VSCode的学习使用
(to be deleted later) yyds, paid academic resources, please keep a low profile!
百度数字人度晓晓在线回应网友喊话 应战上海高考英语作文
Hi3516 full system type burning tutorial
Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition
【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器
浅谈估值模型 (二): PE指标II——PE Band
The IDM server response shows that you do not have permission to download the solution tutorial