在线等,挺急的!js 数组重组 根据配置嵌套结构 形成 父子级数据跪求!

原数组如下
[
[
{"name":"VKORG","value":"1000"},
{"name":"VTWEG","value":"100"},
{"name":"SPART","value":"10"},
{"name":"VKORGTEXT","value":"1000text"},
{"name":"VTWEGTEXT","value":"100text"},
{"name":"SPARTTEXT","value":"10text"}
],
[
{"name":"VKORG","value":"1000"},
{"name":"VTWEG","value":"100"},
{"name":"SPART","value":"9"},
{"name":"VKORGTEXT","value":"1000text"},
{"name":"VTWEGTEXT","value":"100text"},
{"name":"SPARTTEXT","value":"9text"}
],
[
{"name":"VKORG","value":"1252"},
{"name":"VTWEG","value":"100"},
{"name":"SPART","value":"10"},
{"name":"VKORGTEXT","value":"1252text"},
{"name":"VTWEGTEXT","value":"100text"},
{"name":"SPARTTEXT","value":"10text"}
]
]
父子关系是动态的可配置规则展示二级或者三级嵌套如['VKORG','VTWEG','SPART']三级或者['VKORG','VTWEG']二级
如果形成三级父子级数组如下
[
{
'value':'1000',
'label':'1000text',
'children':[{
'value':'100',
'label':'100text',
'children':[{
'value':'10',
'label':'10text'
},
{
'value':'9',
'label':'9text'
}]
}]
},
{
'value':'1252',
'label':'1252text',
'children':[{
'value':'100',
'label':'100text',
'children':[{
'value':'10',
'label':'10text'
}]
}]
}
]
如果形成二级嵌套数组如下
[
{
'value':'1000',
'label':'1000text',
'children':[{
'value':'100',
'label':'100text'
}]
},
{
'value':'1252',
'label':'1252text',
'children':[{
'value':'100',
'label':'100text'
}]
}
]
请问下怎么样根据设置的嵌套层数得到以上结果###题目描述
题目来源及自己的思路
相关代码
//请把代码文本粘贴到下方(请勿用图片代替代码)
你期待的结果是什么?实际看到的错误信息又是什么?
FFIVE
浏览 356回答 2
2回答

慕哥9229398

consttransform=(()=>{constunique=items=>{consthash=newMap();for(constitemofitems){constid=String([item.label,item.value]);if(hash.has(id)){hash.get(id).children.push(...item.children);}else{hash.set(id,item);}}constcollection=[];for(const[,item]ofhash){collection.push(item);if(item.children)item.children=unique(item.children);}returncollection;};returnitems=>{items=items.map(item=>{consthash={};for(const{name,value}ofitem){hash[name]=value;}returnhash;});returnkeys=>{constcollection=items.map(item=>{constcollection=[];for(leti=0,key=keys[i],p=collection;key;key=keys[(i+=1)],p=p[0].children){p.push({children:[],label:item[key+"TEXT"],value:item[key]});}returncollection[0];});returnunique(collection);};};})();constitems=[[{name:"VKORG",value:"1000"},{name:"VTWEG",value:"100"},{name:"SPART",value:"10"},{name:"VKORGTEXT",value:"1000text"},{name:"VTWEGTEXT",value:"100text"},{name:"SPARTTEXT",value:"10text"}],[{name:"VKORG",value:"1000"},{name:"VTWEG",value:"100"},{name:"SPART",value:"9"},{name:"VKORGTEXT",value:"1000text"},{name:"VTWEGTEXT",value:"100text"},{name:"SPARTTEXT",value:"9text"}],[{name:"VKORG",value:"1252"},{name:"VTWEG",value:"100"},{name:"SPART",value:"10"},{name:"VKORGTEXT",value:"1252text"},{name:"VTWEGTEXT",value:"100text"},{name:"SPARTTEXT",value:"10text"}]];consttrans=transform(items);console.log(trans(["VKORG","VTWEG","SPART"]));console.log(trans(["VKORG","VTWEG"]));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript