猿问

大佬们遇到过这个问题吗?已知一个数组,转成另一个数组。数据格式如下

已知数据
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)
没有思路了
胡子哥哥
浏览 226回答 2
2回答

慕容708150

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);

FFIVE

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

相关分类

JavaScript
我要回答