原始的数据是平铺的比如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,//companylevelchildren:[//departmentlevel{label:'AAA-D1',pass:4,onhold:8,children:[//officelevel{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 回答
慕婉清6462132
TA贡献1804条经验 获得超2个赞
答完才发现你要的是能根据指定列进行映射...那我再想想,下面的当参考吧ORZ...varmap=originalData.reduce((p,c)=>{let{company,department,office,pass,onhold}=cif(!p[company])p[company]={}if(!p[company][department])p[company][department]={}p[company][department][office]={pass,onhold}returnp},{})varreduce=(obj,key)=>obj.children.reduce((p,c)=>[p+=c[key],p][1],0)varresult=Object.keys(map).map(cKey=>{letcompany={label:cKey}company.children=Object.keys(map[cKey]).map(dKey=>{letdepartment={label:dKey}department.children=Object.keys(map[cKey][dKey]).map(oKey=>{letoffice=map[cKey][dKey][oKey]return{label:oKey,pass:office.pass,onhold:office.onhold}})['onhold','pass'].forEach(key=>department[key]=reduce(department,key))returndepartment})['onhold','pass'].forEach(key=>company[key]=reduce(company,key))returncompany})输出:[{"label":"AAA","onhold":15,"pass":6"children":[{"label":"AAA-D1","onhold":8,"pass":4"children":[{"label":"AAA-D1-O1","pass":1,"onhold":3},{"label":"AAA-D1-O2","pass":3,"onhold":5}]},{"label":"AAA-D2","onhold":7,"pass":2"children":[{"label":"AAA-D2-O1","pass":2,"onhold":7}]}]},{"label":"BBB","onhold":9,"pass":6"children":[{"label":"BBB-D1","onhold":3,"pass":1"children":[{"label":"BBB-D1-O1","pass":1,"onhold":3}]},{"label":"BBB-D2","onhold":3,"pass":4"children":[{"label":"BBB-D2-O1","pass":4,"onhold":3}]},{"label":"BBB-D3","onhold":3,"pass":1"children":[{"label":"BBB-D3-O1","pass":1,"onhold":3}]}]}]思路上是先把原始数组做一次树级映射,然后再从树生成各层次信息,这样会比较好理解一些。你题目描述中有一些奇怪的地方:companylevel里的department和office的取值标准不清楚,所以就没写,不过这个要加也就是在生成result那一步,创建company时加入节点信息即可,不会很难。
跃然一笑
TA贡献1826条经验 获得超6个赞
你的输出格式应该有问题,起码label:'AAA',department:'AAA-D1',office:'AAA-D1-O1',pass:6,onhold:15,//companylevel是不合理的,最多是label:'AAA',pass:6,onhold:15,//companylevel吧如果数据能够保证全是这样的结构(只有如上的3级),其实还是比较好处理的。varmoriginalData=[{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}];functiona2o(originalData){varoutData=[];varoutObj={};for(vari=0;ivarcompany=originalData[i].company; vardepartment=originalData[i].department;varoffice={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(varcominoutObj){vartmpA={label:com,pass:outObj[com].pass,onhold:outObj[com].onhold,children:[]};for(varj=0;jvartD=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);}returnoutData;}vartreeDate=a2o(moriginalData)console.log(treeDate);
添加回答
举报
0/150
提交
取消