当前位置:网站首页>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
}
边栏推荐
- Gbase 8C trigger (III)
- [principles of multithreading and high concurrency: 1_cpu multi-level cache model]
- 二维格式数组格式索引下标连续问题导致 返回json 格式问题
- JS的装箱和拆箱
- [shutter] bottom navigation bar page frame (bottomnavigationbar bottom navigation bar | pageview sliding page | bottom navigation and sliding page associated operation)
- 怎么将yolov5中的PANet层改为BiFPN
- 【Flutter】shared_ Preferences local storage (introduction | install the shared_preferences plug-in | use the shared_preferences process)
- GBase 8c 函数/存储过程定义
- Apple releases MacOS 11.6.4 update: mainly security fixes
- 面试八股文整理版
猜你喜欢
[advanced ROS] Lesson 6 recording and playback in ROS (rosbag)
Today, it's time to copy the bottom!
定了,就选它
How to change the panet layer in yolov5 to bifpn
SqlServer行转列PIVOT
Baidu map - surrounding search
What does "where 1=1" mean
Linear rectification function relu and its variants in deep learning activation function
Basic operation of binary tree (C language version)
sql server数据库添加 mdf数据库文件,遇到的报错
随机推荐
Compréhension simple de SVG
定了,就选它
Linear rectification function relu and its variants in deep learning activation function
为什么会选择框架?选择什么样的框架
Gbase 8C system table PG_ attribute
Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 2)
[hcia]no.15 communication between VLANs
Your family must be very poor if you fight like this!
Strategy application of Dameng database
Error invalid bound statement (not found): com ruoyi. stock. mapper. StockDetailMapper. XXXX solution
Principle and application of database
Apple releases MacOS 11.6.4 update: mainly security fixes
leetcode540
[advanced ROS] Lesson 6 recording and playback in ROS (rosbag)
Cvpr2022 remove rain and fog
SQL server queries the table structure of the specified table
sql server 查询指定表的表结构
[fluent] future asynchronous programming (introduction | then method | exception capture | async, await keywords | whencomplete method | timeout method)
2022-2028 global splicing display industry research and trend analysis report
SQL statement