当前位置:网站首页>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
}
边栏推荐
- 错误Invalid bound statement (not found): com.ruoyi.stock.mapper.StockDetailMapper.xxxx解决
- Error when installing MySQL in Linux: starting mysql The server quit without updating PID file ([FAILED]al/mysql/data/l.pid
- The difference between left value and right value in C language
- Why choose a frame? What frame to choose
- 【翻译】Flux安全。通过模糊处理获得更多信心
- [principles of multithreading and high concurrency: 1_cpu multi-level cache model]
- Serious security vulnerabilities reported by moxa mxview network management software
- Gbase 8C trigger (I)
- Today, it's time to copy the bottom!
- Gbase 8C system table PG_ cast
猜你喜欢

HW initial preparation

【翻译】后台项目加入了CNCF孵化器

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

C语言初阶-指针详解-庖丁解牛篇
[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)](/img/65/c1fe95f8c391394f7ff1b75c7d75b6.jpg)
[shutter] setup of shutter development environment (supplement the latest information | the latest installation tutorial on August 25, 2021)

Check log4j problems using stain analysis

Joking about Domain Driven Design (III) -- Dilemma

Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 2)

HTB-Devel
随机推荐
Xiaodi notes
处理数据集,使用LabelEncoder将所有id转换为从0开始
GBase 8c系统表-pg_amop
JMeter performance test JDBC request (query database to obtain database data) use "suggestions collection"
簡單理解svg
C语言中左值和右值的区别
The core idea of performance optimization, dry goods sharing
Can netstat still play like this?
[fluent] futurebuilder asynchronous programming (futurebuilder construction method | asyncsnapshot asynchronous calculation)
用docker 连接mysql的过程
Gbase 8C system table PG_ auth_ members
Gbase 8C system table PG_ am
Gbase 8C function / stored procedure parameters (II)
[advanced ROS] Lesson 6 recording and playback in ROS (rosbag)
leetcode540
Add some hard dishes to the interview: how to improve throughput and timeliness in delayed task scenarios!
Source code analysis | resource loading resources
GBase 8c系统表-pg_aggregate
2022-2028 global splicing display industry research and trend analysis report
The Linux server needs to install the agent software EPS (agent) database