当前位置:网站首页>js根据树结构查找某个节点的下面的所有父节点或者子节点
js根据树结构查找某个节点的下面的所有父节点或者子节点
2022-07-03 02:42:00 【华大哥】
我们先看一下树的结构:
[
{
"id":6,
"name":"测试",
"parent_id":1,
"children":[
{
"id":9,
"name":"测试1",
"parent_id":6,
"children":[
{
"id":20,
"name":"测试2",
"parent_id":9,
"children":[
{
"id":21,
"name":"测试3",
"parent_id":20,
},
{
"id":22,
"name":"测试4",
"parent_id":20,
},
{
"id":23,
"name":"测试5",
"parent_id":20,
}
],
}
],
},
]
接着通过递归来实现即可,下面废话不多说,直接上代码:
//查找父节点
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
}
//查找子节点
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
}
边栏推荐
- ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
- GBase 8c 触发器(一)
- Process the dataset and use labelencoder to convert all IDs to start from 0
- Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 2)
- Gbase 8C system table PG_ class
- JMeter performance test JDBC request (query database to obtain database data) use "suggestions collection"
- What does it mean when lambda is not entered?
- leetcode540
- Build a private cloud disk cloudrev
- Getting started | jetpack hilt dependency injection framework
猜你喜欢
Random Shuffle attention
Deep reinforcement learning for intelligent transportation systems: a survey paper reading notes
Error when installing MySQL in Linux: starting mysql The server quit without updating PID file ([FAILED]al/mysql/data/l.pid
HTB-Devel
Matlab tips (24) RBF, GRNN, PNN neural network
Today, it's time to copy the bottom!
面试项目技术栈总结
Random shuffle note
HW initial preparation
[shutter] setup of shutter development environment (supplement the latest information | the latest installation tutorial on August 25, 2021)
随机推荐
Compréhension simple de SVG
Use optimization | points that can be optimized in recyclerview
[advanced ROS] Lesson 6 recording and playback in ROS (rosbag)
[shutter] setup of shutter development environment (supplement the latest information | the latest installation tutorial on August 25, 2021)
leetcode540
【Flutter】shared_ Preferences local storage (introduction | install the shared_preferences plug-in | use the shared_preferences process)
Practice of traffic recording and playback in vivo
[translation] flux is safe. Gain more confidence through fuzzy processing
Add automatic model generation function to hade
Monitoring and management of JVM
Two dimensional format array format index subscript continuity problem leads to return JSON format problem
Basic operation of binary tree (C language version)
Kubernetes cluster log and efk architecture log scheme
GBase 8c系统表-pg_constraint
GBase 8c系统表-pg_amop
Source code analysis | resource loading resources
Gbase 8C system table PG_ authid
Gbase 8C function / stored procedure parameters (I)
Concrete CMS vulnerability
sql server 查詢指定錶的錶結構