• 树状结构


    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);
  • 相关阅读:
    微信·小程序开发工具安装指南及注意事项
    测试
    PC上面的蓝牙的通信(C#)
    关于图片在div中居中问题
    JSONP---跨域请求问题
    关于position的用法
    APICloud自学笔记总结1
    前端html5
    关于图片自适应div大小问题
    亲身经历——大体量公司能为程序员的生涯带来什么帮助?
  • 原文地址:https://www.cnblogs.com/lbx6935/p/12848840.html
Copyright © 2020-2023  润新知