当前位置:网站首页>树数据中根据子节点id查找父节点的id
树数据中根据子节点id查找父节点的id
2022-07-13 17:36:00 【Jason秀啊】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script> var arr = [{
key: '1', children: [ {
key: '1-0', children: [ {
key: '1-0-0' } ] } ] }, {
key: '2', children: [ {
key: '2-0', children: [ {
key: '2-0-0', children: [ {
key: '2-0-0-0' } ] } ] }, {
key: '2-1', children: [ {
key: '2-1-0' } ] } ] }] const getParentId = (treeData, id) => {
let pid = ''; const fn = (treeData, key) => {
for (let i = 0; i < treeData.length; i++) {
const item = treeData[i] if (item.key === id) {
pid = key return; } if (item?.children) fn(item.children, item.key); } }; fn(treeData, id); return pid; }; console.log('father of 2-0-0-0 is:', getParentId(arr, '2-0-0-0')); // father of 2-0-0-0 is: 2-0-0 </script>
</body>
</html>
边栏推荐
猜你喜欢

完整在pycharm上通过torch调用GPU(最细节)

Oil monkey script changes TW style

ES6--string(字符串)

Blue Bridge Cup: the second game of the 13th provincial competition of single chip microcomputer group
![随机信号分析第2版 [赵淑清郑薇编著] (部分)课后作业答案(自己写的)](/img/13/edd5af20fb9a9514339d8ae21eed71.jpg)
随机信号分析第2版 [赵淑清郑薇编著] (部分)课后作业答案(自己写的)
Picture definition problem

<statement> or DELIMITER expected, got ‘id‘

【黄啊码】MySQL入门—2、使用数据定义语言(DDL)操作数据库

Array changes, but the view cannot be updated (reason and thinking)

2022 gopherchina's producers and speech content have been announced···
随机推荐
Dark blue themed boss, can you add a typewriter function so that you can choose when typing
万建峰老师 干货分享2022年7月13日
sql_server2014下载与安装
ES6--类(class)
39.js--作用域
[untitled]
美团基于 Flink 的实时数仓平台建设新进展
JS函数自调用
40.js--同名标识符提升问题
基于 conda 的在 Win10 上从零开始搞一个 Tensorflow-gpu (2.6.0) 环境的操作记录 (2022.07)
ES6--面试题
数据库进阶
Le 13ème concours provincial de Blue Bridge Cup MCU
Installation and use of fiddler in packet capturing tool
[a little knowledge] AQS
ssm项目中下拉框功能的实现
c语言中的输入输出函数之printf函数
ES6--string(字符串)
Idea automatically generates entity classes of corresponding tables in the database
蓝桥杯:单片机组第十三届省赛第二场