猿问

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,//companylevel
children:[//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']不知道怎么写了请大神指导:(:(:(:(:(
慕桂英3389331
浏览 412回答 2
2回答

qq_笑_17

答完才发现你要的是能根据指定列进行映射...那我再想想,下面的当参考吧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时加入节点信息即可,不会很难。

至尊宝的传说

你的输出格式应该有问题,起码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;i
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答