zoukankan      html  css  js  c++  java
  • 树状结构

    1 . 

          let list = [
                    { "yuanshiquhao": 1, "createtime": "2020-04-29T18:31:00", "quhao": 1, "serverid": "1" },
                    { "yuanshiquhao": 2, "createtime": "2020-04-30T10:59:00", "quhao": 1, "serverid": "1" },
                    { "yuanshiquhao": 3, "createtime": "2020-04-30T14:05:00", "quhao": 1, "serverid": "1" },
                    { "yuanshiquhao": 4, "createtime": "2020-04-30T14:14:00", "quhao": 4, "serverid": "1" },
                    { "yuanshiquhao": 5, "createtime": "2020-04-30T14:16:00", "quhao": 5, "serverid": "1" },
                    { "yuanshiquhao": 6, "createtime": "2020-04-30T14:17:00", "quhao": 7, "serverid": "1" },
                    { "yuanshiquhao": 7, "createtime": "2020-04-30T14:19:00", "quhao": 7, "serverid": "1" }
                ]

            var newlist = list.filter(c => c.quhao == c.yuanshiquhao);


            newlist.filter(f => {

              let cnode = list.filter(c => c.quhao == f.quhao);
              let del = cnode.filter(c => c.yuanshiquhao != f.yuanshiquhao);

              el.length > 0 ? (f.children = del) : "";
            });

    
    
                console.log(newlist);

    2. 

     let place = [
                    {
                        "id": "1",
                        "name": "中国",
                        "code": "110",
                        "parent": ""
                    },
                    {
                        "id": "2",
                        "name": "北京市",
                        "code": "110000",
                        "parent": "110"
                    },
                    {
                        "id": "3",
                        "name": "河北省",
                        "code": "130000",
                        "parent": "110"
                    },
                    {
                        "id": "4",
                        "name": "四川省",
                        "code": "510000",
                        "parent": "110"
                    },
                    {
                        "id": "5",
                        "name": "石家庄市",
                        "code": "130001",
                        "parent": "130000"
                    },
                    {
                        "id": "6",
                        "name": "唐山市",
                        "code": "130002",
                        "parent": "130000"
                    },
                    {
                        "id": "7",
                        "name": "邢台市",
                        "code": "130003",
                        "parent": "130000"
                    },
                    {
                        "id": "8",
                        "name": "成都市",
                        "code": "510001",
                        "parent": "510000"
                    },
                    {
                        "id": "9",
                        "name": "简阳市",
                        "code": "510002",
                        "parent": "510000"
                    },
                    {
                        "id": "10",
                        "name": "武侯区",
                        "code": "51000101",
                        "parent": "510001"
                    },
                    {
                        "id": "11",
                        "name": "金牛区",
                        "code": "51000102",
                        "parent": "510001"
                    }
                ];
                place = place.filter(father => {
                    //每次都会寻找符合father.code的数据
                    let childNode = place.filter(child => child.parent == father.code);
                    childNode.length > 0 ? father.children = childNode : "";
                    return father.parent == "";
                });
                console.log(place);
  • 相关阅读:
    网上购物瘾,你怎么能退出?
    POJ 1006 Biorhythms 中国的法律来解决剩余的正式
    【Android接口实现】PhotoView——单点支持/多图像缩放,实现了触摸
    线程同步synchronized
    阿里云CentOS 6.5 设备、执行Docker容器和步骤的方法
    打破了中国电信华为无线路由猫(HG522-C)自己主动拨号+任意数量的计算机+iTV
    GCC 命令行具体解释
    Nginx 负载均衡
    Linux pipe功能
    Java有用的经验--Swing片
  • 原文地址:https://www.cnblogs.com/lbx6935/p/12848840.html
Copyright © 2011-2022 走看看