JS数据转成父子节点数据

原始的数据是平铺的 比如


originalData: [

        { company: 'AAA', department:'AAA-D1', office: 'AAA-D1-O1', pass: 1, onhold: 3},

        { company: 'AAA', department:'AAA-D1', office: 'AAA-D1-O2', pass: 3, onhold: 5},

        { company: 'AAA', department:'AAA-D2', office: 'AAA-D2-O1', pass: 2, onhold: 7},

        { company: 'BBB', department:'BBB-D1', office: 'BBB-D1-O1', pass: 1, onhold: 3},

        { company: 'BBB', department:'BBB-D2', office: 'BBB-D2-O1', pass: 4, onhold: 3},

        { company: 'BBB', department:'BBB-D3', office: 'BBB-D3-O1', pass: 1, onhold: 3},

        ....

        ....

      ]

希望转换成


treeData: [

    { label: 'AAA', department:'AAA-D1', office: 'AAA-D1-O1', pass: 6, onhold: 15,   // company level 

        children: [  // department level 

            {label: 'AAA-D1',  pass: 4, onhold: 8,

                children: [  // office level

                    {label: 'AAA-D1-O1', pass: 1, onhold: 3},

                    {label: 'AAA-D1-O2', pass: 1, onhold: 3},

                ]

            },

            {label: 'AAA-D2',  pass: 2, onhold: 7,

                children: [

                    {label: 'AAA-D2-O1', pass: 1, onhold: 3},

                ]

            },

        ]

    },

    ...

    ...

]

    

根据指定的列 转换成父子结构的数据 比如 上面的是根据 ['company', 'department', 'office'] 不知道怎么写了 请大神指导 :( :( :( :( :(


翻阅古今
浏览 534回答 2
2回答

阿晨1998

你的输出格式应该有问题,起码label: 'AAA', department:'AAA-D1', office: 'AAA-D1-O1', pass: 6, onhold: 15, // company level 是不合理的,最多是label: 'AAA', pass: 6, onhold: 15, // company level 吧如果数据能够保证全是这样的结构(只有如上的3级),其实还是比较好处理的。var moriginalData= [&nbsp; &nbsp; &nbsp; &nbsp; { company: 'AAA', department:'AAA-D1', office: 'AAA-D1-O1', pass: 1, onhold: 3},&nbsp; &nbsp; &nbsp; &nbsp; { company: 'AAA', department:'AAA-D1', office: 'AAA-D1-O2', pass: 3, onhold: 5},&nbsp; &nbsp; &nbsp; &nbsp; { company: 'AAA', department:'AAA-D2', office: 'AAA-D2-O1', pass: 2, onhold: 7},&nbsp; &nbsp; &nbsp; &nbsp; { company: 'BBB', department:'BBB-D1', office: 'BBB-D1-O1', pass: 1, onhold: 3},&nbsp; &nbsp; &nbsp; &nbsp; { company: 'BBB', department:'BBB-D2', office: 'BBB-D2-O1', pass: 4, onhold: 3},&nbsp; &nbsp; &nbsp; &nbsp; { company: 'BBB', department:'BBB-D3', office: 'BBB-D3-O1', pass: 1, onhold: 3}&nbsp; &nbsp; &nbsp; ];function a2o(originalData){&nbsp; &nbsp; var outData=[];&nbsp; &nbsp; var outObj={};&nbsp; &nbsp; for(var i=0;i<originalData.length;i++){&nbsp; &nbsp; &nbsp; &nbsp; var company= originalData[i].company;&nbsp; &nbsp; &nbsp; &nbsp; var department= originalData[i].department;&nbsp; &nbsp; &nbsp; &nbsp; var office={label:originalData[i].office, pass:originalData[i].pass, onhold:originalData[i].onhold};&nbsp; &nbsp; &nbsp; &nbsp; if(outObj[company]===undefined){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outObj[company]={childrenKey:[], pass:0, onhold:0};&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(outObj[company][department]===undefined){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outObj[company][department]={children:[], pass:0, onhold:0 };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outObj[company].childrenKey.push(department)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; outObj[company][department].children.push(office);&nbsp; &nbsp; &nbsp; &nbsp; outObj[company][department].pass=outObj[company][department].pass+office.pass;&nbsp; &nbsp; &nbsp; &nbsp; outObj[company].pass=outObj[company].pass+office.pass;&nbsp; &nbsp; &nbsp; &nbsp; outObj[company][department].onhold=outObj[company][department].onhold+office.onhold;&nbsp; &nbsp; &nbsp; &nbsp; outObj[company].onhold=outObj[company].onhold+office.onhold;&nbsp; &nbsp; }&nbsp; &nbsp; for( var com in outObj){&nbsp; &nbsp; &nbsp; &nbsp; var tmpA={label:com, pass:outObj[com].pass, onhold:outObj[com].onhold, children:[]};&nbsp; &nbsp; &nbsp; &nbsp; for (var j=0; j<outObj[com].childrenKey.length; j++ ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var tD=outObj[com][ outObj[com].childrenKey[j] ];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpA.children.push({label:outObj[com].childrenKey[j] ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pass:tD.pass ,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onhold:tD.onhold ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; children:tD.children} );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; outData.push(tmpA);&nbsp; &nbsp; }&nbsp; &nbsp; return outData;}&nbsp; &nbsp;var treeDate=a2o(moriginalData)console.log(treeDate);&nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript