当前位置:网站首页>自上而下获取父节点的id的集合
自上而下获取父节点的id的集合
2022-07-13 17:36:00 【Jason秀啊】
自下而上获取父节点的id的集合,包括它自己的id
例如:查找根据西湖区的id查找浙江省id,杭州市id,并根据这些id组成一个集合
思路
- 从根节点开始递归遍历,直到找到西湖区,每遍历一次就保存一次id
实现
const getParentIds = <T, K>(treeData: T[], key: K): string[] => {
let pids: string[] = [];
const fn = (treeData: T[], key: K, id?: string) => {
if (id) pids.push(id);
treeData.forEach((item: {
[key: string]: any }) => {
if (item.key === key) {
pids.push(item.key);
return;
}
if (item?.children) fn(item.children as T[], key as K, item.key as string);
});
};
fn(treeData, key);
return pids;
};
边栏推荐
猜你喜欢

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

油猴脚本更改tw樣式

Online multi line text batch regular replacement add suffix tool

Idea自动化生成数据库中对应表的实体类

41.js--闭包

36.js--原型链2--(笔试题为主)

Implementation of drop-down box function in SSM project

Idea automatically exports SQL statements of tables in the database

Teacher Wan Jianfeng dry goods sharing July 13, 2022

sql_ Server2014 download and installation
随机推荐
2022暑期实践(PowerDesigner教程学习记录)(第一周)
C语言:通过结构体实现通讯录的简易功能
Exploration and practice of integration of streaming and wholesale in jd.com
Changement de style TW du script singe huileux
Micro service mode: sidecar
Random signal analysis, 2nd Edition [edited by Zhao Shuqing and Zheng Wei] (part) answer to homework after class (written by myself)
【黄啊码】今天居然有人问我:where 1=1 是什么意思?
【Tensorflow2】AttributeError: ‘Tensor‘ object has no attribute ‘numpy‘ 解决 (tf.py_function)
解决IDEA2020连接MySQL8出现unable to load authentication plugin ‘caching_sha2_password‘异常
蓝桥杯:单片机组第十三届省赛第二场
遍历器总结
PV operation procedure questions
ES6--对象
ES6--Set
c语言中的输入输出函数之scanf函数
JS函数自调用
Based on the use of PageHelper paging plug-in in SSM project (easy to use)
Teacher Wan Jianfeng dry goods sharing July 13, 2022
ES6--symbol( )与Map( )
vue+axios+mysql实现分页查询,条件查询以及批量删除