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);
  • 相关阅读:
    旅行喵 React Native 技术实践
    微信、QQ这类IM App怎么做——谈谈Websocket
    IOS热更新-JSPatch实现原理+Patch现场恢复
    jquery 插件开发及extend
    JS Nice – JavaScript 代码美化和格式化工具
    ExtJS与jQuery的一点细节上的对比
    JQuery实现图片的预加载与延时加载
    十个实用但IE不支持的CSS属性
    如何通过预加载器提升网页加载速度
    遭遇Asp.Net长文件名下载的问题和解决办法
  • 原文地址:https://www.cnblogs.com/lbx6935/p/12848840.html
Copyright © 2011-2022 走看看