constskutitle=[{label:"颜色",prop:"val",},{label:"尺寸",prop:"val1",},{label:"形状",prop:"val2",},{label:"规格",prop:"val3",}]//表格头consttableData=[{val:"红",val1:"大",val2:"五边形",val3:"1*222",price:"22"},{val:"绿",val1:"大",val2:"6边形",val3:"1*2224",price:"2244"},{val:"蓝",val1:"大",val3:"1*225",val2:"八边形",price:"2244"}]constall=[{'颜色':"蓝",'尺寸':"大",'形状':"八边形",'规格':"1*225",'价格':"2244"}]skutitle数组和tableData数组转成all数组那种数据格式得,目前代码如下functionarrayList(skutitle,tableData){varresult=[]for(leti=0;i
2 回答
沧海一幻觉
TA贡献1824条经验 获得超5个赞
你的表格头数据差一个价格,实现的方法如下,你看能否符合你的预期:constsku2Obj=skutitle.reduce((total,cur)=>{total[cur.prop]=cur.label;returntotal;},{});constall=tableData.reduce((result,cur)=>{letobj=Object.keys(cur).reduce((buffResult,buffCur)=>{buffResult[sku2Obj[buffCur]]=cur[buffCur];returnbuffResult;},{});result.push(obj);returnresult;},[]);console.log(all);
繁星coding
TA贡献1797条经验 获得超4个赞
functiontransform(tableData,skutitle){returntableData.map(data=>{constkeys=Object.keys(data);constnewKeys=keys.map(key=>getLabelByProp(key,skutitle));constnewData={};newKeys.forEach((key,i)=>{newData[key]=data[keys[i]];});returnnewData;});functiongetLabelByProp(prop,skutitle){for(leti=0;iif(skutitle[i].prop===prop)returnskutitle[i].label; }returnprop;}}
添加回答
举报
0/150
提交
取消