当前位置:网站首页>Processing tree structured data
Processing tree structured data
2022-07-24 07:03:00 【MMNHD】
// Encapsulate tree data processing
// The core idea : The process of finding the parent node adopt pid To match id If it matches, put the current item push To match chilren Properties of the
// Fixed implementation steps :
// 1. Loop through groups In the form of id As key, Each item itself acts as value Form a new object [ It is convenient to find the parent node ]
// 2. Traversal array With each item in the array pid Go to match the object formed in the first step id If it matches It means its child nodes
// push To chilren Properties of the If it doesn't match Represents itself as the outermost parent node direct push To the tree array of the final output
/**
const map = {
'1': {
id:1,
pid:null
},
'2':{
id:2
pid:1
}
}
const arr = [
{
id:1,
pid:null
},
{
pid:1,
id:2
}
]
*/
function transTree(arr) {
const treeArr = []
// Logical processing
const map = {}
arr.forEach((item) => {
map[item.id] = item
map[item.id].children = []
})
arr.forEach(item => {
// Object value taking skills If pid As key You can get the value representative Match up
if (map[item.pid]) {
map[item.pid].children.push(item)
} else {
treeArr.push(item)
}
})
return treeArr
}
export default transTree
// Is there any other way
// Time complexity o(n)
// Recursive writing The function itself calls As long as there are function calls Stack pressing operation Once the maximum memory is exceeded, there will be stack overflow
边栏推荐
- [lvgl (important)] style attribute API function and its parameters
- [lvgl (5)] label usage
- [C language] operator details (in-depth understanding + sorting and classification)
- PostgreSQL date handler usage
- Redis特殊数据类型-HyperLogLog
- Redis 主从机制
- OWASP TOP10 penetration test
- Introduction to pyqt5 - student management system
- Redis入门
- 安装snownlp包过程出现Requirement already satisfied:及Read timed out.问题解决方法
猜你喜欢
![[lvgl (2)]](/img/f8/d04183cf74896295382765a9dfd88d.png)
[lvgl (2)]
![[USB voltmeter and ammeter] Based on stm32f103c8t6 for Arduino](/img/5f/40e4e144a628ef1aa38ab536b40366.png)
[USB voltmeter and ammeter] Based on stm32f103c8t6 for Arduino

Ue4/5 cannot open the file "xxx.generated.h" (cannot open file xxx.generated.h) solution summary

Create WPF project

不运动也能增肌???打一针冬眠黑熊的血清就行

STM32基于hal库的adc以DMA的多通道采样以及所遇问题解决

tensorflow scatter_nd函数

数据分析思维之从整体出发分析零售行业——全方位多方面细节分析

安装snownlp包过程出现Requirement already satisfied:及Read timed out.问题解决方法

Tensorflow Einstein function
随机推荐
Redis特殊数据类型-HyperLogLog
Don't care too much about others' eyes, it will erase your glory
处理树形结构数据
After grouping, return to the last record group in each group_ Use of concat
2022-07-22 mysql/stonedb parallel hashjoin memory usage analysis
vs2019配置运行open3d例子
Redis.conf details
Gangster escape 3
不运动也能增肌???打一针冬眠黑熊的血清就行
Redis.conf详解
5. Template cache. Drawing a square can only move within the triangle
MapReduce (I)
Three level classification / menu query tree structure
你是谁由你自己决定!
sojson jsjiami.com. V6 crawler JS reverse
项目问题积累
三级分类/菜单的查询——树形结构
owasp top10 渗透测试
Prediction of advertising investment and sales based on regression analysis -- K neighborhood, decision tree, random forest, linear regression, ridge regression
济南人社已签1W+电子劳动合同,法大大助力HR数字化