已知数据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;i
2 回答
至尊宝的传说
TA贡献1789条经验 获得超10个赞
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);
慕盖茨4494581
TA贡献1850条经验 获得超11个赞
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"}]);
添加回答
举报
0/150
提交
取消