当前位置:网站首页>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
}
边栏推荐
- [shutter] setup of shutter development environment (supplement the latest information | the latest installation tutorial on August 25, 2021)
- 迅雷chrome扩展插件造成服务器返回的数据js解析页面数据异常
- Gbase 8C system table PG_ am
- Add MDF database file to SQL Server database, and the error is reported
- Random shuffle note
- Gbase 8C function / stored procedure parameters (I)
- Why choose a frame? What frame to choose
- Use cve-2021-43893 to delete files on the domain controller
- 面试项目技术栈总结
- Check log4j problems using stain analysis
猜你喜欢

Random Shuffle attention

Didi programmers are despised by relatives: an annual salary of 800000 is not as good as two teachers
![ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc](/img/cb/145937a27ef08050a370d5a255215a.jpg)
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc

Can netstat still play like this?

Matlab tips (24) RBF, GRNN, PNN neural network

Today, it's time to copy the bottom!

Kubernetes family container housekeeper pod online Q & A?
![[shutter] bottom navigation bar page frame (bottomnavigationbar bottom navigation bar | pageview sliding page | bottom navigation and sliding page associated operation)](/img/6e/67bc187a89fb9125856c78c89f7bfb.gif)
[shutter] bottom navigation bar page frame (bottomnavigationbar bottom navigation bar | pageview sliding page | bottom navigation and sliding page associated operation)

《MATLAB 神经网络43个案例分析》:第43章 神经网络高效编程技巧——基于MATLAB R2012b新版本特性的探讨

Deep reinforcement learning for intelligent transportation systems: a survey paper reading notes
随机推荐
Interview stereotyped version
[fluent] futurebuilder asynchronous programming (futurebuilder construction method | asyncsnapshot asynchronous calculation)
Matlab tips (24) RBF, GRNN, PNN neural network
Summary of interview project technology stack
JS的装箱和拆箱
GBase 8c 触发器(一)
Use cve-2021-43893 to delete files on the domain controller
[principles of multithreading and high concurrency: 1_cpu multi-level cache model]
HTB-Devel
Check log4j problems using stain analysis
where 1=1 是什么意思
Unity3d human skin real time rendering real simulated human skin real time rendering "suggestions collection"
[flutter] example of asynchronous programming code between future and futurebuilder (futurebuilder constructor setting | handling flutter Chinese garbled | complete code example)
[shutter] setup of shutter development environment (supplement the latest information | the latest installation tutorial on August 25, 2021)
Apple releases MacOS 11.6.4 update: mainly security fixes
Apple releases MacOS 11.6.4 update: mainly security fixes
Pytest (6) -fixture (Firmware)
Packing and unpacking of JS
Baidu map - surrounding search
Random shuffle note