当前位置:网站首页>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
}
边栏推荐
- A2L file parsing based on CAN bus (2)
- Add MDF database file to SQL Server database, and the error is reported
- Matlab tips (24) RBF, GRNN, PNN neural network
- 基于can总线的A2L文件解析(2)
- GBase 8c系统表-pg_amop
- Random shuffle note
- [tutorial] chrome turns off cross domain policies CORS and samesite, and brings cookies across domains
- Strategy application of Dameng database
- Check log4j problems using stain analysis
- Hcip137-147 title + analysis
猜你喜欢

Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)

Oauth2.0 authentication, login and access "/oauth/token", how to get the value of request header authorization (basictoken)???

Check log4j problems using stain analysis

Kubernetes family container housekeeper pod online Q & A?

Pytest (6) -fixture (Firmware)

The Linux server needs to install the agent software EPS (agent) database

Random Shuffle attention
![[translation] the background project has joined the CNCF incubator](/img/0b/e3d2674b1a1cba3ea398cbcb1a018a.png)
[translation] the background project has joined the CNCF incubator

【翻译】后台项目加入了CNCF孵化器
![[fluent] listview list (map method description of list set | vertical list | horizontal list | code example)](/img/e5/c01f760b07b495f5b048ea367e0c21.gif)
[fluent] listview list (map method description of list set | vertical list | horizontal list | code example)
随机推荐
簡單理解svg
Wechat - developed by wechat official account Net core access
How to return ordered keys after counter counts the quantity
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
GBase 8c 触发器(一)
Gbase 8C function / stored procedure parameters (I)
Gbase 8C create user / role example 2
Your family must be very poor if you fight like this!
Principle and application of database
The solution of "the required function is not supported" in win10 remote desktop connection is to modify the Registry [easy to understand]
【富瀚6630编码存录像,用rtsp服务器及时间戳同步实现vlc观看录像】
The difference between left value and right value in C language
Cron表达式介绍
JS的装箱和拆箱
为什么会选择框架?选择什么样的框架
Didi programmers are despised by relatives: an annual salary of 800000 is not as good as two teachers
random shuffle注意
处理数据集,使用LabelEncoder将所有id转换为从0开始
Gbase 8C system table PG_ cast
Three.js本地环境搭建