树状结构

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);

          del.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);

 

posted @ 2020-05-08 10:09  进阶的哈姆雷特  阅读(175)  评论(0编辑  收藏  举报