js 遍历tree的一个例子(全遍历)
全遍历
亲测真是有效。
工作中遇到的问题应该算是比较有价值的问题。
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head> <body>
<script>
var a = [
{
"id": 1,
"name": "轻应用管理",
"childrenIds": "10,11,12,13",
"parentId": 0,
"orderNum": 1,
"url": "/lightApp/light/list",
"description": "轻应用管理",
"createTime": "2018-09-19 17:18:25",
"modifyTime": "2018-09-21 11:17:54",
"status": "ENABLE",
"mustUse": "NO",
"own": true,
"childrens": [
{
"id": 10,
"name": "轻应用列表",
"childrenIds": null,
"parentId": 1,
"orderNum": 1,
"url": "/lightApp/light/list",
"description": "轻应用列表",
"createTime": "2018-09-19 17:25:49",
"modifyTime": "2018-09-19 17:25:49",
"status": "ENABLE",
"mustUse": "NO",
"own": true,
"childrens": null
},
{
"id": 11,
"name": "轻应用信息审核",
"childrenIds": null,
"parentId": 1,
"orderNum": 2,
"url": "/lightApp/auditInfo/list",
"description": "轻应用信息审核",
"createTime": "2018-09-19 17:26:28",
"modifyTime": "2018-09-19 17:26:28",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": null
},
{
"id": 12,
"name": "轻应用开发审核",
"childrenIds": null,
"parentId": 1,
"orderNum": 3,
"url": "/lightApp/auditDevelop/list",
"description": "轻应用开发审核",
"createTime": "2018-09-19 17:26:52",
"modifyTime": "2018-09-19 17:26:52",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": null
},
{
"id": 13,
"name": "轻应用接口审核",
"childrenIds": null,
"parentId": 1,
"orderNum": 4,
"url": "/lightApp/auditInterface/list",
"description": "轻应用接口审核",
"createTime": "2018-09-19 17:27:12",
"modifyTime": "2018-09-19 17:27:12",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": null
}
]
},
{
"id": 2,
"name": "小程序管理",
"childrenIds": "17,18,15,14,16",
"parentId": 0,
"orderNum": 2,
"url": "/applet/builtIn/list",
"description": "小程序管理",
"createTime": "2018-09-19 17:19:36",
"modifyTime": "2018-09-19 17:19:36",
"status": "ENABLE",
"mustUse": "NO",
"own": true,
"childrens": [
{
"id": 14,
"name": "内置小程序",
"childrenIds": null,
"parentId": 2,
"orderNum": 1,
"url": "/applet/builtIn/list",
"description": "内置小程序",
"createTime": "2018-09-19 17:27:55",
"modifyTime": "2018-09-19 17:27:55",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": null
},
{
"id": 15,
"name": "外置小程序",
"childrenIds": null,
"parentId": 2,
"orderNum": 2,
"url": "/applet/externalSet/list",
"description": "外置小程序",
"createTime": "2018-09-19 17:28:27",
"modifyTime": "2018-09-19 17:28:27",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": null
},
{
"id": 16,
"name": "小程序信息审核",
"childrenIds": null,
"parentId": 2,
"orderNum": 3,
"url": "/applet/auditInfo/list",
"description": "小程序信息审核",
"createTime": "2018-09-19 17:29:17",
"modifyTime": "2018-09-19 17:29:17",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": null
},
{
"id": 17,
"name": "小程序开发审核",
"childrenIds": null,
"parentId": 2,
"orderNum": 4,
"url": "/applet/auditDevelop/list",
"description": "小程序开发审核",
"createTime": "2018-09-19 17:29:52",
"modifyTime": "2018-09-19 17:29:52",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": null
},
{
"id": 18,
"name": "小程序接口审核",
"childrenIds": null,
"parentId": 2,
"orderNum": 5,
"url": "/applet/appletInterface/list",
"description": "小程序接口审核",
"createTime": "2018-09-19 17:30:22",
"modifyTime": "2018-09-19 17:30:22",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": null
}
]
},
{
"id": 3,
"name": "模板管理",
"childrenIds": "19,20",
"parentId": 0,
"orderNum": 3,
"url": "/template/message/list",
"description": "模板管理",
"createTime": "2018-09-19 17:20:14",
"modifyTime": "2018-09-19 17:20:14",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": [
{
"id": 19,
"name": "消息模板管理",
"childrenIds": null,
"parentId": 3,
"orderNum": 1,
"url": "/template/message/list",
"description": "消息模板管理",
"createTime": "2018-09-19 17:31:29",
"modifyTime": "2018-09-19 17:31:29",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": null
},
{
"id": 20,
"name": "订单模板管理",
"childrenIds": null,
"parentId": 3,
"orderNum": 2,
"url": "/template/order/list",
"description": "订单模板管理",
"createTime": "2018-09-19 17:32:01",
"modifyTime": "2018-09-19 17:32:01",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": null
}
]
},
{
"id": 4,
"name": "项目管理",
"childrenIds": "21,22,23",
"parentId": 0,
"orderNum": 4,
"url": "/projects/project/list",
"description": "项目管理",
"createTime": "2018-09-19 17:20:38",
"modifyTime": "2018-09-19 17:20:38",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": [
{
"id": 21,
"name": "项目列表",
"childrenIds": null,
"parentId": 4,
"orderNum": 1,
"url": "/projects/project/list",
"description": "项目列表",
"createTime": "2018-09-19 17:32:44",
"modifyTime": "2018-09-19 17:32:44",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": null
},
{
"id": 22,
"name": "项目应用管理",
"childrenIds": null,
"parentId": 4,
"orderNum": 2,
"url": "/projects/application/list",
"description": "项目应用管理",
"createTime": "2018-09-19 17:33:11",
"modifyTime": "2018-09-19 17:33:11",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": null
},
{
"id": 23,
"name": "项目版本管理",
"childrenIds": null,
"parentId": 4,
"orderNum": 3,
"url": "/projects/versions/list",
"description": "项目版本管理",
"createTime": "2018-09-19 17:33:32",
"modifyTime": "2018-09-19 17:33:32",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": null
}
]
},
{
"id": 5,
"name": "信息管理",
"childrenIds": "24",
"parentId": 0,
"orderNum": 5,
"url": "/information/announcement/list",
"description": "信息管理",
"createTime": "2018-09-19 17:20:47",
"modifyTime": "2018-09-19 17:20:47",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": [
{
"id": 24,
"name": "公告列表",
"childrenIds": null,
"parentId": 5,
"orderNum": 1,
"url": "/information/announcement/list",
"description": "公告列表",
"createTime": "2018-09-19 17:34:00",
"modifyTime": "2018-09-19 17:34:00",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": null
}
]
},
{
"id": 6,
"name": "用户管理",
"childrenIds": "25,26,27,28",
"parentId": 0,
"orderNum": 6,
"url": "/user/registerRecord/list",
"description": "用户管理",
"createTime": "2018-09-19 17:21:02",
"modifyTime": "2018-09-19 17:21:02",
"status": "ENABLE",
"mustUse": "NO",
"own": true,
"childrens": [
{
"id": 25,
"name": "用户注册记录",
"childrenIds": null,
"parentId": 6,
"orderNum": 1,
"url": "/user/registerRecord/list",
"description": "用户注册记录",
"createTime": "2018-09-19 17:34:26",
"modifyTime": "2018-09-19 17:34:26",
"status": "ENABLE",
"mustUse": "NO",
"own": true,
"childrens": null
},
{
"id": 26,
"name": "认证申请管理",
"childrenIds": null,
"parentId": 6,
"orderNum": 2,
"url": "/user/certificationApplication/list",
"description": "认证申请管理",
"createTime": "2018-09-19 17:34:41",
"modifyTime": "2018-09-19 17:34:41",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": null
},
{
"id": 27,
"name": "开发者认证管理",
"childrenIds": null,
"parentId": 6,
"orderNum": 3,
"url": "/user/developerCertification/list",
"description": "开发者认证管理",
"createTime": "2018-09-19 17:35:29",
"modifyTime": "2018-09-19 17:35:29",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": null
},
{
"id": 28,
"name": "登录日志",
"childrenIds": null,
"parentId": 6,
"orderNum": 4,
"url": "/user/loginLog/list",
"description": "登录日志",
"createTime": "2018-09-19 17:36:03",
"modifyTime": "2018-09-19 17:36:03",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": null
}
]
},
{
"id": 7,
"name": "终端管理",
"childrenIds": "29",
"parentId": 0,
"orderNum": 7,
"url": "/terminal/terminal/list",
"description": "终端管理",
"createTime": "2018-09-19 17:21:18",
"modifyTime": "2018-09-19 17:21:18",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": [
{
"id": 29,
"name": "终端管理",
"childrenIds": null,
"parentId": 7,
"orderNum": 1,
"url": "/terminal/terminal/list",
"description": "终端管理",
"createTime": "2018-09-19 17:36:34",
"modifyTime": "2018-09-19 17:36:34",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": null
}
]
},
{
"id": 8,
"name": "基础配置",
"childrenIds": "30",
"parentId": 0,
"orderNum": 8,
"url": "/basics/appAttribute/list",
"description": "基础配置",
"createTime": "2018-09-19 17:21:34",
"modifyTime": "2018-09-19 17:21:34",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": [
{
"id": 30,
"name": "应用属性管理",
"childrenIds": null,
"parentId": 8,
"orderNum": 1,
"url": "/basics/appAttribute/list",
"description": "应用属性管理",
"createTime": "2018-09-19 17:36:57",
"modifyTime": "2018-09-19 17:36:57",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": null
}
]
},
{
"id": 9,
"name": "系统管理",
"childrenIds": "31,32",
"parentId": 0,
"orderNum": 9,
"url": null,
"description": "系统管理",
"createTime": "2018-09-19 17:22:41",
"modifyTime": "2018-09-19 17:22:41",
"status": "ENABLE",
"mustUse": "YES",
"own": false,
"childrens": [
{
"id": 31,
"name": "角色管理",
"childrenIds": null,
"parentId": 9,
"orderNum": 1,
"url": null,
"description": "角色管理",
"createTime": "2018-09-19 17:40:02",
"modifyTime": "2018-09-19 17:40:02",
"status": "ENABLE",
"mustUse": "NO",
"own": false,
"childrens": null
},
{
"id": 32,
"name": "权限管理",
"childrenIds": null,
"parentId": 9,
"orderNum": 2,
"url": null,
"description": "权限管理",
"createTime": "2018-09-19 17:40:42",
"modifyTime": "2018-09-19 17:40:42",
"status": "ENABLE",
"mustUse": "NO",
"own": true,
"childrens": null
}
]
}
];
var tickMenuIdFilter = (function () {
var resultArr = new Array();
var getTickMenuId = function (obj) {
if (undefined == obj || null == obj || !obj instanceof Object) {
return;
}
if (obj.own) {
resultArr.push(obj.id);
}
if (null != obj.childrens && obj.childrens instanceof Array) {
for (let child of obj.childrens) {
getTickMenuId(child);
}
} } return {
filter: function (arr) {
if (!arr instanceof Array) {
return false;
}
resultArr = new Array();
for (let rootMenu of arr) {
getTickMenuId(rootMenu);
}
return resultArr;
}
}
})();
var b = tickMenuIdFilter.filter(a);
console.log("a", a);
console.log("b", b); //结果:1,10,2,32
</script>
</body> </html>
js 遍历tree的一个例子(全遍历)的更多相关文章
- js 遍历tree的一个例子(全遍历),更复杂的功能
更复杂的功能 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- js 遍历tree的一个例子
小例子蕴含着到力量,我希望自己有改变自己生活的能力,自己有主宰自己命运的能力... 备注:这个例子有问题. <!DOCTYPE html> <html lang="en&q ...
- 【Node.js】4.从一个例子切入Node js的规范
在开始之前,需要明确的一点就是, ①Node应用是由模块组成的,每一个文件都是一个模块,有自己的作用域. ②在这个文件里定义的变量,函数,类都是私有的,对其他的文件不可见. ③在一个文件中,也就是在一 ...
- JS中for...in循环陷阱及遍历数组的方式对比
JavaScript中有很多遍历数组的方式,比较常见的是for(var i=0;i<arr.length;i++){},以及for...in...循环等,这些遍历都有各自的优缺点,下面来看看各种 ...
- HDFS 读取、写入、遍历文件夹获取文件全路径、append
版权声明:本文为博主原创文章,未经博主同意不得转载.安金龙 的博客. https://blog.csdn.net/smile0198/article/details/37573081 1.从HDFS中 ...
- 【Offer】[8] 【中序遍历的下一个结点】
题目描述 思路分析 Java代码 代码链接 题目描述 给定一棵二叉树和其中的一个结点,如何找出中序遍历顺序的下一个结点? 树中的结点除了有两个分别指向左右子结点的指针以外,还有一个指向父结点的指针. ...
- PAT 1043 Is It a Binary Search Tree (25分) 由前序遍历得到二叉搜索树的后序遍历
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- 【数据结构与算法Python版学习笔记】树——树的遍历 Tree Traversals
遍历方式 前序遍历 在前序遍历中,先访问根节点,然后递归地前序遍历左子树,最后递归地前序遍历右子树. 中序遍历 在中序遍历中,先递归地中序遍历左子树,然后访问根节点,最后递归地中序遍历右子树. 后序遍 ...
- 原生js使用forEach()与jquery使用each遍历数组,return false 的区别
原生js使用forEach()与jquery使用each()遍历数组,return false 的区别: 1.使用each()遍历数组a,如下: var a=[20,21,22,23,24]; $.e ...
随机推荐
- 利用XmlDocument操作XML文件
利用XmlDocument可以方便的操作XML文件. .操作XML文件基本方法 ()添加对System.Xml的引用,并使用using语句添加引用: ()假设要读取的XML文件如下: <?xml ...
- 记一次完整的android源码截屏事件的捕获<标记砖>
http://blog.csdn.net/buptgshengod/article/details/19911909?utm_source=tuicool&utm_medium=referra ...
- css垂直居中方法(一)
第一种方法:首先用margin:0 auto实现水平居中,然后设置position:relative,设置top为50%(父元素高度的50%),然后设置margin-top:-150px(设置负值是因 ...
- 2.2.3 Analyzing the output 分析对用户推荐书目的结果(2)
2.2.3 Analyzing the output 在之前的程序运行结果中我们得到的结果输出是: RecommendedItem [item:104, value:4.257081] 程序要 ...
- Codeforces 464E The Classic Problem (最短路 + 主席树 + hash)
题意及思路 这个题加深了我对主席树的理解,是个好题.每次更新某个点的距离时,是以之前对这个点的插入操作形成的线段树为基础,在O(logn)的时间中造出了一颗新的线段树,相比直接创建n颗线段树更省时间. ...
- 线段树教做人系列(1)HDU4967 Handling the Past
题意:给你n组操作,分别为压栈,出栈,询问栈顶元素.每一组操作有一个时间戳,每次询问栈顶的元素的操作询问的是在他之前出现的操作,而且时间戳小于它的情况.题目中不会出现栈为空而且出栈的情况. 例如: p ...
- 使用ServerSocket建立聊天服务器(一)
-------------siwuxie095 工程名:TestMyServerSocket 包名:com.siwuxie095.socket ...
- (转)嵌入式C开发人员的最好笔试题目
约定: 1) 下面的测试题中,认为所有必须的头文件都已经正确的包含了 2)数据类型 char 一个字节 1 byte int 两个字节 2 byte ( ...
- 3D模型浏览器的实现思路
前段时间正好浏览了数据结构中关于图的部分,突然就意识到一个问题,3D模型就是用无向图来存储的.仔细想一想是不是这样呢? 一个3D模型去掉材质之后剩下的部分就是点以及点和点之间的连线了,点我们用三维坐标 ...
- ZROI2018普转提day6t1
传送门 分析 记录区间最大值,线段树上二分找比这个点大的最靠前位置即可 代码 #include<iostream> #include<cstdio> #include<c ...