为了账号安全,请及时绑定邮箱和手机立即绑定

JS数据转成父子节点数据

JS数据转成父子节点数据

翻阅古今 2019-03-12 17:15:57
原始的数据是平铺的 比如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'] 不知道怎么写了 请大神指导 :( :( :( :( :(
查看完整描述

2 回答

?
阿晨1998

TA贡献2037条经验 获得超6个赞

你的输出格式应该有问题,起码

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= [

        { 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}

      ];

function a2o(originalData){

    var outData=[];

    var outObj={};

    for(var i=0;i<originalData.length;i++){

        var company= originalData[i].company;

        var department= originalData[i].department;

        var office={label:originalData[i].office, pass:originalData[i].pass, onhold:originalData[i].onhold};

        if(outObj[company]===undefined){

            outObj[company]={childrenKey:[], pass:0, onhold:0};

        }

        if(outObj[company][department]===undefined){

            outObj[company][department]={children:[], pass:0, onhold:0 };

            outObj[company].childrenKey.push(department)

        }

        outObj[company][department].children.push(office);


        outObj[company][department].pass=outObj[company][department].pass+office.pass;

        outObj[company].pass=outObj[company].pass+office.pass;


        outObj[company][department].onhold=outObj[company][department].onhold+office.onhold;

        outObj[company].onhold=outObj[company].onhold+office.onhold;

    }

    for( var com in outObj){

        var tmpA={label:com, pass:outObj[com].pass, onhold:outObj[com].onhold, children:[]};

        for (var j=0; j<outObj[com].childrenKey.length; j++ ){

            var tD=outObj[com][ outObj[com].childrenKey[j] ];

            tmpA.children.push({label:outObj[com].childrenKey[j] ,

                                pass:tD.pass , 

                                onhold:tD.onhold ,

                                children:tD.children} );

        }

        outData.push(tmpA);

    }

    return outData;

}   


var treeDate=a2o(moriginalData)

console.log(treeDate);  


查看完整回答
反对 回复 2019-04-04
  • 2 回答
  • 0 关注
  • 533 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信