js 递归总结
1.根据子id 递归查找所有父级 id 主要用于vue element 中 Cascader 级联选择器展示 在编辑中回显默认展示
tree 数据
var arr = [{
"label": "文件夹",
"parentId": null,
"id": "0",
"children": [{
"label": "文件夹1",
"parentId": "0",
"id": "1",
"children": [{
"label": "文件夹1-1",
"parentId": "1",
"id": "1.1",
"children": null
},
{
"label": "文件夹1-2",
"parentId": "1",
"id": "1.2",
"children": null
},
{
"label": "文件夹1-3",
"parentId": "1",
"id": "1.3",
"children": [{
"label": "文件夹1-3-1",
"parentId": "1.3",
"id": "1.3.1",
"children": [{
"label": "文件夹1-3-1-1",
"parentId": "1.3.1",
"id": "1.3.1.1",
"children": null
}]
},
{
"label": "文件夹1-3-2",
"parentId": "1.3",
"id": "1.3.2",
"children": null
},
{
"label": "文件夹1-3-3",
"parentId": "1.3",
"id": "1.3.3",
"children": null
}
]
}
]
},
{
"label": "文件夹2",
"parentId": "0",
"id": "2",
"children": [{
"label": "文件夹2-1",
"parentId": "2",
"id": "2.1",
"children": null
}]
},
{
"label": "文件夹3",
"parentId": "0",
"id": "3",
"children": null
} ]
}];
递归查找
//递归找父级id
getParentIds(treeData, nodeId) {
var arrRes = [];
if(treeData.length == 0) {
if(!!nodeId) {
arrRes.unshift(nodeId);
}
return arrRes;
}
let rev = (data, nodeId) => {
for(var i = 0, length = data.length; i < length; i++) {
let node = data[i];
if(node.id == nodeId) {
arrRes.unshift(nodeId);
rev(treeData, node.parentId);
break;
} else {
if(!!node.children) {
rev(node.children, nodeId);
}
}
}
return arrRes;
};
arrRes = rev(treeData, nodeId);
return arrRes;
},
结果
console.log(getParentIds(arr,'1.3.1')) //["0", "1", "1.3", "1.3.1"]
2. 删除children:[] 存在且为空的情况 主要用于vue element 中 Cascader 级联选择器展示
var tree = [{
value: 'ziyuan',
label: '资源',
children: [
{
value: 'axure',
label: 'Axure Components',
children: []
},
{
value: 'sketch',
label: 'Sketch Templates',
children: []
},
{
value: 'jiaohu',
label: '组件交互文档',
children: []
}
]
},
{
value: 'zujian',
label: '组件',
children: [
{
value: 'layout',
label: 'Layout 布局',
children: []
},
{
value: 'icon',
label: 'Icon 图标',
children: []
} ]
}
]; function render(arr) {
for(let i = 0; i < arr.length; i++) {
if(arr[i] && arr[i].children.length == 0) {
delete arr[i].children
}
if(arr[i].children && arr[i].children.length > 0) {
render(arr[i].children)
}
}
return arr
} console.log(render(tree))
//打印如下:
已经删除空的children
js 递归总结的更多相关文章
- js递归遍历多维数组并在修改数组的key后返回新的多维数组
我司最近正在用VUE做一个基于用户权限显示不同左侧菜单的后台管理系统,接口会根据用户的权限不同返回不同的菜单名称.URL等,前端要将这些菜单名称及URL动态添加到系统的左侧,这里就用到了vue-rou ...
- 【Vue.js实战案例】- Vue.js递归组件实现组织架构树和选人功能
大家好!先上图看看本次案例的整体效果. 浪奔,浪流,万里涛涛江水永不休.如果在jq时代来实这个功能简直有些噩梦了,但是自从前端思想发展到现在的以MVVM为主流的大背景下,来实现一个这样繁杂的功能简直不 ...
- js 递归调用
js递归调用 function fact(num) { ) { ; } else { ); } } 以下代码可导致出错: var anotherFact = fact; fact = null; al ...
- 关于js回调方法 js递归时使用方法
js中递归调用本身可以这样: function a1(n){ a1(n)}但是如果需要在参数n进行自增的情况下判断会出错: function a1(n){ if(n>10) return 'aa ...
- Vue.js 递归组件实现树形菜单
最近看了 Vue.js 的递归组件,实现了一个最基本的树形菜单. 项目结构: main.js 作为入口,很简单: import Vue from 'vue' Vue.config.debug = tr ...
- js 递归学习
作用:将一些复制的算法变为简单,比如:(举例子)计算数组 var a =[1,3,4,6,7,8]的长度:求 5!的值,也可以做搜索用等. //求数组的长度function len(arry){ i ...
- js递归错误
错误信息:Uncaught RangeError: Maximum call stack size exceeded 问题代码: js代码:$(function(){ selectTog ...
- 用Vue.js递归组件构建一个可折叠的树形菜单
在Vue.js中一个递归组件调用的是其本身,如: Vue.component('recursive-component', { template: `<!--Invoking myself! ...
- Vue.js递归组件实现动态树形菜单
使用Vue递归组件实现动态菜单 现在很多项目的菜单都是动态生成的,之前自己做项目也是遇到这种需求,翻看了官网案例,和网上大神的案例.只有两个感觉,官网的案例太简洁,没有什么注释,看起来不太好理解,大神 ...
- js递归获取html页面所有标签
js原生递归获取,直接源码 : <script> var child = document.children; var arr = [];//用来存放获取到的所有的标签 function ...
随机推荐
- windows环境下使用python3.x自带的CGI服务器测试cgi脚本(转)
1.在桌面上新建一个文件夹作为服务器目录文件夹(文件夹名称自定义,文件夹位置自定义),在www文件下再建一个文件夹,文件夹名为“cgi-bin”,须是这个文件名,其他试过不行(原因暂时未知)
- spring boot配置spring-data-jpa的时候报错CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NoSuchMethodError
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager f ...
- 客户端TNSPING通 连接出现ORA-12514错误
ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务,这是一个经常遇到的问题,可以按照以下步骤一步步解决 1.使用tnsping检测 tnsping可判断出以下两点(1)判断网络 ...
- Python里使用转义字符\r时遇到的问题
在Pycharm里使用转义字符\r和在IDLE里使用\r产生的结果是不一样的. 例子如下: print("你好!\r我是Python!") 输出结果为: 我是Python! 前面的 ...
- Redis(七):set/sadd/sismember/sinter/sdiffstore 命令源码解析
上两篇我们讲了hash和list数据类型相关的主要实现方法,同时加上前面对框架服务和string相关的功能介绍,已揭开了大部分redis的实用面纱. 现在还剩下两种数据类型: set, zset. 本 ...
- 【原创】(一)Linux进程调度器-基础
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...
- VLC for CentOS7
https://blog.csdn.net/qiuyoujie/article/details/78486947 http://elearning.wsldp.com/pcmagazine/insta ...
- X-CTF(REVERSE高级) Reversing-x64Elf-100
逻辑很简单,如果sub_4006FD函数返回假则返回Nice! 图1 进入sub_4006FD函数,加密过程也很简单,这里值得注意的有两点 一.8*(i%3)是二维数组的第一个参数,这里是取v3的地址 ...
- Docker基础内容之命令大全
run(未补全) 说明:创建一个新的容器并运行一个命令 语法如下: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] 选项说明: -a stdin: 指定标准 ...
- Idea-LifecycleException when deploying
案例 今天第一次用idea构建项目,出现了如下问题: FAIL - Application at context path /myWebApp could not be started FAIL - ...