javascript 将treeNode 转换id和pid的Array
function treeTolist(treeNodes, opt) { if (!opt) {
opt = {};
opt.key = "id";
opt.parent = "pid";
opt.children = "children";
} var NodeList = []; function appenChildren(nodes) { for (var i = 0; i < nodes.length; i++) { var node = nodes[i]; //如果没有上层节点那么就是根节点
if (node.parentNode == null) {
node.Level = 0;
node[opt.parent]=0;
}
//判断是否有子节点
if (node[opt.children] && node[opt.children].length > 0) {
//所有子节点
for (var k = 0; k < node.children.length; k++) {
node[opt.children][k][opt.parent] = node[opt.parent];
node[opt.children][k].Level = node.Level + 1;
node[opt.children][k].parentNode = node;
} appenChildren(node.children); }
if(node.hasOwnProperty("parentNode"))
delete node.parentNode;
if(node.hasOwnProperty(opt.children))
delete node[opt.children];
NodeList.push(node); }
} appenChildren(treeNodes); return NodeList; } var TreeNodes = [
{
"id": 1,
"parent": 0,
"children": [
{
"id": 2,
"parent": 1,
"children": [
{
"id": 3,
"parent": 2,
"children": [
{
"id": 4,
"parent": 3,
"children": [
{
"id": 5,
"parent": 4,
"children": [
{
"id": 6,
"parent": 5,
"children": [
{
"id": 7,
"parent": 6,
"children": [
{
"id": 8,
"parent": 7,
"children": [
{
"id": 9,
"parent": 8,
"children": [
{
"id": 10,
"parent": 9,
"children": []
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]; var treeNodeList = treeTolist(TreeNodes, {
key: "id",
parent: "pid",
children: "children"
});
console.log(JSON.stringify(treeNodeList));
技术交流QQ群:15129679
javascript 将treeNode 转换id和pid的Array的更多相关文章
- ztree树id、pid转成children格式的(待整理完整)
山铝菜单 因为菜单选用了bootstrap treeview ,而格式需要是children类似的格式 var nodes = [ {name: "父节点1", children: ...
- JavaScript通过父节点ID递归生成JSON树
JavaScript通过父节点ID递归生成JSON树: · 实现思路:通过递归实现(第一次递归的时候查询出所有的父节点,然后通过当前父节点id不断地去查询所有子节点,直到递归完毕返回) · 代码示 ...
- javascript的数值转换
在javascript中数值转换,最要的一点是函数第一个字母必须要大写.js中的函数有string字符型.number数值型.null空型.boolean布尔型.undefined未定义. 具体的转换 ...
- JSON的基本格式以及与JavaScript之间的转换
JSON的基本格式以及与JavaScript之间的转换 近来,发现很多人写json格式的数据时,总是没有达到JSON的规范,虽然在语言要求不严格的情况下能够通过, 但是,难免会遇到这样那样的问题,到时 ...
- javascript获取select 的id与值
javascript获取select 的id与值 <script type="text/javascript"> function showOptionId () { ...
- 每天多一点(2016.12.04)》Javascript隐式转换
乱想 javascript为什么需要隐式转换?如果没有会出现什么情况? 找了一圈没有看到关于这个的讨论,只好自己研究了,可能不一定正确,自行辨知. 郁闷就是郁闷在好好的,为什么要搞个隐式转换,一般来讲 ...
- [译]JavaScript源码转换:非破坏式与再生式
原文:http://ariya.ofilabs.com/2013/06/javascript-source-transformation-non-destructive-vs-regenerative ...
- 如何在脚本中获取进程ID(PID)
我想要知道运行中脚本子shell的进程id.我该如何在shell脚本中得到PID. 当我在执行shell脚本时,它会启动一个叫子shell的进程.作为主shell的子进程,子shell将shell脚本 ...
- Javascript隐式转换
乱想 javascript为什么需要隐式转换?如果没有会出现什么情况? 找了一圈没有看到关于这个的讨论,只好自己研究了,可能不一定正确,自行辨知. 郁闷就是郁闷在好好的,为什么要搞个隐式转换,一般来讲 ...
随机推荐
- USBDM RS08/HCS08/HCS12/Coldfire V1,2,3,4/DSC/Kinetis Debugger and Programmer -- BDM Construction and Firmware
Construction. Build the hardware using the information provided in the PCB download. The following a ...
- centos中安装tomcat+jenkins
1) 安装tomcat 安装tomcat6: http://www.cnblogs.com/itech/p/3506011.html 安装tomcat7: http://www.cnblogs.com ...
- DTrace Probes in HotSpot VM
http://docs.oracle.com/javase/6/docs/technotes/guides/vm/dtrace.html
- EF Core数据迁移操作
摘要 在开发中,使用EF code first方式开发,那么如果涉及到数据表的变更,该如何做呢?当然如果是新项目,删除数据库,然后重新生成就行了,那么如果是线上的项目,数据库中已经有数据了,那么删除数 ...
- sql 语句注意括号配对
- excel 鼠标上下左右移动
.Offset用法:(如果是多选单元格,偏移后选定的依然是区域) Selection.Offset(-1).select 'up Selection.Offset(1).select 'down ...
- 提高你的代码稳定性与可读性-lint工具
from://http://wiki.eoe.cn/page/Improving_Your_Code_with_lint.html 负责人:lingzideshensha 分任务原文链接:http:/ ...
- IP视频通信中的"丢包恢复技术”(LPR)
转自:http://blog.csdn.net/blade2001/article/details/9094709 在IP视频通话中,即使是在丢包率很小的情况下也会对使用效果造成较为明显的影响.正是由 ...
- 机器学习的MLE和MAP:最大似然估计和最大后验估计
https://zhuanlan.zhihu.com/p/32480810 TLDR (or the take away) 频率学派 - Frequentist - Maximum Likelihoo ...
- warning: LF will be replaced by CRLF in dubbo-demo-api/pom.xml.
今天使用git add .的时候出现了一个错误. 错误如下: 解决方案: $ rm -rf .git // 删除.git $ git config --global core.autocrlf fal ...