当前位置:网站首页>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
}
边栏推荐
- Practice of traffic recording and playback in vivo
- SQL server queries the table structure of the specified table
- Gbase 8C system table PG_ constraint
- Linear rectification function relu and its variants in deep learning activation function
- Random shuffle note
- Joking about Domain Driven Design (III) -- Dilemma
- Kubernetes family container housekeeper pod online Q & A?
- Source code analysis | resource loading resources
- Add some hard dishes to the interview: how to improve throughput and timeliness in delayed task scenarios!
- What does "where 1=1" mean
猜你喜欢

怎么将yolov5中的PANet层改为BiFPN

迅雷chrome扩展插件造成服务器返回的数据js解析页面数据异常

Didi programmers are despised by relatives: an annual salary of 800000 is not as good as two teachers

【翻译】具有集中控制平面的现代应用负载平衡

【Flutter】shared_ Preferences local storage (introduction | install the shared_preferences plug-in | use the shared_preferences process)

Linear rectification function relu and its variants in deep learning activation function

Check log4j problems using stain analysis

Your family must be very poor if you fight like this!
![[hcia]no.15 communication between VLANs](/img/59/a467c5920cbccb72040f39f719d701.jpg)
[hcia]no.15 communication between VLANs
![[translation] the background project has joined the CNCF incubator](/img/0b/e3d2674b1a1cba3ea398cbcb1a018a.png)
[translation] the background project has joined the CNCF incubator
随机推荐
random shuffle注意
[fluent] listview list (map method description of list set | vertical list | horizontal list | code example)
sql server 查詢指定錶的錶結構
Tensorflow to pytorch notes; tf. gather_ Nd (x, y) to pytorch
where 1=1 是什么意思
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
Gbase 8C system table PG_ constraint
SQL statement
面试项目技术栈总结
搭建私有云盘 cloudreve
Your family must be very poor if you fight like this!
The Linux server needs to install the agent software EPS (agent) database
【富瀚6630编码存录像,用rtsp服务器及时间戳同步实现vlc观看录像】
基于can总线的A2L文件解析(2)
【翻译】后台项目加入了CNCF孵化器
[fluent] futurebuilder asynchronous programming (futurebuilder construction method | asyncsnapshot asynchronous calculation)
超好用的日志库 logzero
[translation] modern application load balancing with centralized control plane
疫情当头,作为Leader如何进行代码版本和需求开发管控?| 社区征文
Practice of traffic recording and playback in vivo