当前位置:网站首页>JS finds all the parent nodes or child nodes under a node according to the tree structure
JS finds all the parent nodes or child nodes under a node according to the tree structure
2022-07-03 02:46:00 【Brother Hua】
Let's take a look at the structure of the tree first :
[
{
"id":6,
"name":" test ",
"parent_id":1,
"children":[
{
"id":9,
"name":" test 1",
"parent_id":6,
"children":[
{
"id":20,
"name":" test 2",
"parent_id":9,
"children":[
{
"id":21,
"name":" test 3",
"parent_id":20,
},
{
"id":22,
"name":" test 4",
"parent_id":20,
},
{
"id":23,
"name":" test 5",
"parent_id":20,
}
],
}
],
},
]
Then it can be achieved by passing it back , There's no more nonsense , Go straight to the code :
// Find the parent node
parentTreeNode (treeList, id) {
var temp = []
var searchFn = function (treeList, id) {
for (var i = 0; i < treeList.length; i++) {
var item = treeList[i]
if (item.id === id) {
temp.push(item)
searchFn(treeList, item.parent_id)
break
} else {
if (item.children) {
searchFn(item.children, id)
}
}
}
}
searchFn(treeList, id)
return temp
}
// Find child nodes
childrenTreeNode (treeList, id) {
var temp = []
var searchFn = function (treeList, id) {
for (var i = 0; i < treeList.length; i++) {
var item = treeList[i]
if (item.parent_id === id) {
temp.push(item)
searchFn(treeList, item.id)
break
} else {
if (item.children) {
searchFn(item.children, id)
}
}
}
}
searchFn(treeList, id)
return temp
}
边栏推荐
- Add some hard dishes to the interview: how to improve throughput and timeliness in delayed task scenarios!
- Monitoring and management of JVM
- [shutter] bottom navigation bar page frame (bottomnavigationbar bottom navigation bar | pageview sliding page | bottom navigation and sliding page associated operation)
- sql server 查詢指定錶的錶結構
- Gbase 8C system table PG_ authid
- Why choose a frame? What frame to choose
- Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)
- [fluent] JSON model conversion (JSON serialization tool | JSON manual serialization | writing dart model classes according to JSON | online automatic conversion of dart classes according to JSON)
- JMeter performance test JDBC request (query database to obtain database data) use "suggestions collection"
- tensor中的append应该如何实现
猜你喜欢
Super easy to use logzero
random shuffle注意
Deep reinforcement learning for intelligent transportation systems: a survey paper reading notes
【翻译】后台项目加入了CNCF孵化器
Error invalid bound statement (not found): com ruoyi. stock. mapper. StockDetailMapper. XXXX solution
面试项目技术栈总结
Random shuffle note
Baidu map - surrounding search
Choose it when you decide
A2L file parsing based on CAN bus (2)
随机推荐
Gbase 8C system table PG_ collation
xiaodi-笔记
"Analysis of 43 cases of MATLAB neural network": Chapter 43 efficient programming skills of neural network -- Discussion Based on the characteristics of the new version of MATLAB r2012b
C语言初阶-指针详解-庖丁解牛篇
【Flutter】shared_ Preferences local storage (introduction | install the shared_preferences plug-in | use the shared_preferences process)
Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 2)
Principle and application of database
Packing and unpacking of JS
左值右指解释的比较好的
Gbase 8C system table PG_ amproc
Kubernetes family container housekeeper pod online Q & A?
简单理解svg
Pytest (6) -fixture (Firmware)
Gbase 8C function / stored procedure parameters (I)
[Hcia]No.15 Vlan间通信
【 tutoriel】 Chrome ferme les cors et les messages de la politique inter - domaines et apporte des cookies à travers les domaines
Gbase 8C function / stored procedure parameters (II)
Practice of traffic recording and playback in vivo
用docker 连接mysql的过程
Baidu map - surrounding search