/**
* 将list装换成tree
* @param {Object} myId 数据主键id
* @param {Object} pId 数据关联的父级id
* @param {Object} list list集合
*/
function listToTree(myId,pId,list){
function exists(list, parentId){
for(var i=0; i<list.length; i++){
if (list[i][myId] == parentId) return true;
}
return false;
} var nodes = [];
// get the top level nodes
for(var i=0; i<list.length; i++){
var row = list[i];
if (!exists(list, row[pId])){
nodes.push(row);
}
} var toDo = [];
for(var i=0; i<nodes.length; i++){
toDo.push(nodes[i]);
}
while(toDo.length){
var node = toDo.shift(); // the parent node
// get the children nodes
for(var i=0; i<list.length; i++){
var row = list[i];
if (row[pId] == node[myId]){
//var child = {id:row.id,text:row.name};
if (node.children){
node.children.push(row);
} else {
node.children = [row];
}
toDo.push(row);
}
}
}
return nodes;
} var list=[
{"ids":1,"parendId":0,"name":"Foods",url:"wwww"},
{"ids":2,"parentId":1,"name":"Fruits"},
{"ids":3,"parentId":1,"name":"Vegetables"},
{"ids":4,"parentId":2,"name":"apple"},
{"ids":5,"parentId":2,"name":"orange"},
{"ids":6,"parentId":3,"name":"tomato"},
{"ids":7,"parentId":3,"name":"carrot"},
{"ids":8,"parentId":3,"name":"cabbage"},
{"ids":9,"parentId":3,"name":"potato"},
{"ids":10,"parentId":3,"name":"lettuce"}, {"ids":11,"parendId":0,"name":"Foods"},
{"ids":12,"parentId":11,"name":"Fruits"},
{"ids":13,"parentId":11,"name":"Vegetables"},
{"ids":14,"parentId":12,"name":"apple"},
{"ids":15,"parentId":12,"name":"orange"},
{"ids":16,"parentId":13,"name":"tomato"},
{"ids":17,"parentId":13,"name":"carrot"},
{"ids":18,"parentId":13,"name":"cabbage"},
{"ids":19,"parentId":13,"name":"potato"},
{"ids":20,"parentId":13,"name":"lettuce"}
]; console.log(JSON.stringify(listToTree("ids","parentId",list)));
console.log(listToTree("ids","parentId",list));

朝朝暮暮 终于写出来了,好几年前的心愿了

javascript将list转换成树状结构的更多相关文章

  1. PHP将数据集转换成树状结构

    /** * 把返回的数据集转换成Tree * @param array $list 要转换的数据集 * @param string $pid parent标记字段 * @param string $l ...

  2. js修改父子json格式成树状结构格式

    js修改父子json成树状结构 var json = [ { "id" : "01", "pId":"" } , { & ...

  3. linux 把ls -R格式化成树状结构

    谁能写脚本把linux中的ls -R命令的结果格式化成树状结构? 最好是shell脚本!欢迎讨论! 参与讨论有可能意外获取iPhone6哦~~

  4. js中把ajax获取的数据转化成树状结构(并做成多级联动效果)

    1.首先通过ajax获取数据,此处省略,直接贴出获取到的数据格式 var arr = [{ id: 1, name: "一级标题", pid: 0 }, { id: 2, name ...

  5. php 二维数组转换成树状数组(转)

    <?php/** * @param array $list 要转换的结果集 * @param string $pid parent标记字段 * @param string $level leve ...

  6. 05 - json转成树状结构

    var jsonData = eval(`[ {"id":"4","pid":"1","name": ...

  7. iOS:二叉树多级表格的使用,使用三方库TreeTableView-master实现对json解析数据的递归遍历整理成树状结构

    在项目中,我们有时需要使用二叉树来实现多级表格的递归遍历查询,如果对二叉树比较懂,那么写起来其实也不费事,为了节省开发时间,下面介绍一下第三方库TreeTableView-master,这个三方库上给 ...

  8. 使用Map辅助拼装树状结构,消除递归调用

    目前菜单或其他树状结构在数据库中的存储,多数是以一个parentid作为关联字段,以一维形式存储.使用时全部查询出来,然后在内存中拼装成树状结构.现在主要涉及的是拼装方法的问题. 一般可以进行 递归调 ...

  9. 原生JS实现树状结构列表

    树状结构列表,这个技术点之前有写过了,是基于vue讲解,但似乎都没有解决痛点,最基础的原生JS该怎么实现呢? 这篇文章会全面详细的介绍树状结构列表的实现,从数据处理成树状结构,到动态生成dom节点渲染 ...

随机推荐

  1. Serv-U设置被动模式(FTP工具)

    FTP服务器在公司内网,通过端口映射把21端口映射出去. 公司一些机器也在各个省的机房内网.好在这些机器可以访问公网.由于各个地区的机器托管在各个地区机房. 我有公司防火墙的权限,可以做防火墙上做端口 ...

  2. windows python监听文件触发脚本

    from watchdog.events import * class FileEventHandler(FileSystemEventHandler): def __init__(self): Fi ...

  3. thinkphp5 列表页数据分页查询-带搜索条件

    一.控制器部分 <?php namespace app\user\controller; use app\user\model\HelpCenterManual as HelpCenterMan ...

  4. OpenWrt实现802.11s组网模式

    参考 http://www.docin.com/p-277067204.html 无线网卡wlan0正常后,输入一下命令 iw dev wlan0 interface add mesh_iface t ...

  5. ML: 降维算法-LE

    PCA的降维原则是最小化投影损失,或者是最大化保留投影后数据的方差.LDA降维需要知道降维前数据分别属于哪一类,而且还要知道数据完整的高维信息.拉普拉斯特征映射 (Laplacian Eigenmap ...

  6. 弄清SDI显示工程中的每一个信号,每一个逻辑

    弄清SDI显示工程中的每一个信号,每一个逻辑 1. FIFO外部逻辑控制 FIFO的读和写在不同的时钟域,所以读和写的控制逻辑应当分开写在不同的两个always块语句中. 2.播出端复位信号的产生 : ...

  7. 用swoole实现mysql的连接池--摘自https://github.com/153734009/doc/blob/master/php/mysql_pool.php

    <?php   $serv = new swoole_server("0.0.0.0", 9508);   $serv->set(['worker_num'=>1 ...

  8. 根据时间段获取时间段内所有时间点(js)

    Date.prototype.format=function (){var s='';s+=this.getFullYear()+'-';// 获取年份.s+=(this.getMonth()+1)+ ...

  9. jquery.nicescroll.min.js滚动条使用方法

    jquery.nicescroll.min.js滚动条使用方法,Nicescroll 是制作自定义滚动条的jq插件.支持div,iframe,html等使用,兼容IE7-8,safari,firefo ...

  10. Docker镜像构建上下文(Context)

    镜像构建上下文(Context) Dicker在构建镜像时,如果注意,会看到 docker build 命令最后有一个 ... 表示当前目录,而 Dockerfile 就在当前目录,因此不少初学者以为 ...