JS组装数据结构

开始的数据结构如下:里面的工程数据是一条条的....

   var reg ={
        data: [
             {
                xmId: "c8bfc7f4-153c-4f85-b6bd-e0b47856ca0f",
                xmName: "新建武汉至十堰高速铁路",
                xmShortName: "汉十高铁",
                projectId: "23a8dcdf-ad9b-4c34-8807-02e6fad2cdf4",
                projectName: "安陆府河特大桥",
                buildTypeCode: "Q",
                jcb: "HSJC-2",
                jlb: "HSJL-1",
                sgb: "HSSG-1",
                total: 4
            },
            {
                xmId: "c8bfc7f4-153c-4f85-b6bd-e0b47856ca0f",
                xmName: "新建武汉至十堰高速铁路",
                xmShortName: "汉十高铁",
                projectId: "ce574f2d-8f55-4f5c-8850-b37d01e123fc",
                projectName: "安陆府河特大桥(DK091+092.615)十堰台(DK094+009.44~DK094+030.46)",
                buildTypeCode: "T",
                jcb: "HSJC-2",
                jlb: "HSJL-1",
                sgb: "HSSG-1",
                total: 3
            },
            {
                xmId: "c6bfc7f4-153c-4f85-b6bd-e0b47856ca0f",
                xmName: "新建武汉至十堰高速铁路",
                xmShortName: "汉十高铁11111",
                projectId: "ce574f2d-8f55-4f5c-8850-b37d01e123fc",
                projectName: "云安特大桥",
                buildTypeCode: "T",
                jcb: "HSJC-2",
                jlb: "HSJL-1",
                sgb: "HSSG-1",
                total: 35
            }
        ],
        code: 0,
        message: "操作成功"
    };

现在要做的是把项目ID相同的工程放在同一项目下--project数组里-数据结构如下:

 var data = [
        {
            xmId: "c8bfc7f4-153c-4f85-b6bd-e0b47856ca0f",
            xmName: "新建武汉至十堰高速铁路",
            xmShortName: "汉十高铁",
            project: [
                {
                projectId: "23a8dcdf-ad9b-4c34-8807-02e6fad2cdf4",
                projectName: "安陆府河特大桥",
                buildTypeCode: "Q",
                jcb: "HSJC-2",
                jlb: "HSJL-1",
                sgb: "HSSG-1",
                total: 4
            },
                {
                    projectId: "ce574f2d-8f55-4f5c-8850-b37d01e123fc",
                    projectName: "安陆府河特大桥(DK091+092.615)十堰台(DK094+009.44~DK094+030.46)",
                    buildTypeCode: "T",
                    jcb: "HSJC-2",
                    jlb: "HSJL-1",
                    sgb: "HSSG-1",
                    total: 3
                }
            ]
        },

        {
            xmId: "a1fd685f-ec44-4d46-8d4d-7b35c0ca08ca",
            xmName: "新建襄阳至武汉高速铁路",
            xmShortName: "襄武高铁",
            project: [{
                projectId: "23b8dcdf-ad9b-4c34-8807-02e6fad2cdf4",
                projectName: "襄武第一段",
                buildTypeCode: "Q",
                jcb: "HSJC-2",
                jlb: "HSJL-1",
                sgb: "HSSG-1",
                total: 4
            },
                {
                    projectId: "cd574f2d-8f55-4f5c-8850-b37d01e123fc",
                    projectName: "襄武第三段",
                    buildTypeCode: "T",
                    jcb: "HSJC-2",
                    jlb: "HSJL-1",
                    sgb: "HSSG-1",
                    total: 3
                }
            ]
        }
    ];

实现代码如下:

var planData = [];
                          var map =[];
                          var data = reg.data;
                        for(var i=0;i<reg.data.length;i++){
                            var proData= {};
                            var xmData = {};
                            xmData.project = [];
                            var n = data[i].xmId;
                            xmData.xmId = data[i].xmId;
                            xmData.xmShortName = data[i].xmShortName;
                            proData.projectId = data[i].projectId;
                            proData.projectName = data[i].projectName;
                            proData.buildTypeCode = data[i].buildTypeCode;
                            proData.sgb = data[i].sgb;
                            proData.total = data[i].total;
                            if(!map[n]){
                                map[n] = data[i].total;
                                xmData.project.push(proData); //可能有问题
                                planData.push(xmData);
                            }
                            else{ //项目ID已存在
                                for(var j = 0; j < planData.length; j++){
                                    var aj = planData[j];
                                    if(aj.xmId == n){
                                        planData[j].project.push(proData);
                                        break;
                                    }
                                }
                            }
                        }  

          console.log(
planData);

 

posted @ 2017-07-10 09:12  千寻的天空之城  阅读(976)  评论(0编辑  收藏  举报