• 树状结构


    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);
  • 相关阅读:
    Java解惑之TreeSet是如何去重的
    Spring.profiles多环境配置最佳实践
    设计模式-单例模式的5种实现
    JAVA实现单双向链表的增、删、改、查
    RxJava/RxAndroid 使用实例实践
    数学模型与计算机科学的认知
    Mac下TensorFlow安装及环境搭建
    2017年Android百大框架排行榜
    python 多线程就这么简单
    python 内置模块之hashlib、hmac、uuid
  • 原文地址:https://www.cnblogs.com/lbx6935/p/12848840.html
Copyright © 2020-2023  润新知