已知一个数组,转成另一个数组。数据格式如下大佬们有什么好的建议?

已知数据
vartableData=[
{val:"红",val1:"大",val2:"五边形",val3:"1*222"},
{val:"绿",val1:"大",val2:"6边形",val3:"1*2224"},
{val:"红",val1:"大",val2:"五边形",val3:"1*222"}
]
预期结果
varresult=[['红','绿'],['大'],['五边形','6边形'],["1*222","1*222"]]
目前做法varcc=[]
tableData.map((item,index)=>{
console.log(item,index)
vararr=[]
varobj={}
for(varkeyinitem){
if(key!=='price'){
obj[key]=item[key]
arr.push(obj[key])
console.log(arr)
}
console.log(key,item[key])
}
cc.push(arr)
})
console.log(cc)
vardd=[];
for(vari=0;ivarkk=[];
console.log(cc[i])
for(varj=0;jkk.push(cc[i][j])
}
console.log(kk)
dd.push(kk)
}
console.log(dd)
没有思路了
慕桂英546537
浏览 268回答 2
2回答

繁星点点滴滴

vartableData=[{val:"红",val1:"大",val2:"五边形",val3:"1*222"},{val:"绿",val1:"大",val2:"6边形",val3:"1*2224"},{val:"红",val1:"大",val2:"五边形",val3:"1*222"}]lettemp=tableData.reduce((temp,item)=>{Object.keys(item).forEach(key=>{if(!temp[key])temp[key]={}temp[key][item[key]]=''})returntemp},{})letres=Object.keys(temp).map(r=>Object.keys(temp[r]))console.log(res);

慕勒3428872

functiontranslate(tableData){letkeyArr=['val','val1','val2','val3'];lettempResult=keyArr.map(item=>{returntableData.map(_item=>{return_item[item];})});console.log(tempResult);returntempResult.map(item=>{return[...newSet(item)];})}//测试translate([{val:"红",val1:"大",val2:"五边形",val3:"1*222"},{val:"绿",val1:"大",val2:"6边形",val3:"1*2224"},{val:"红",val1:"大",val2:"五边形",val3:"1*222"}]);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript