vararr1=['name','age','home']vararr2=[['a','b','c','d','e'],[1,2,3,4,5],['one','two','three','four','five']]vararr3=[{'name':'a','age':1,'home':'one'},{'name':'b','age':2,'home':'two'},{'name':'c','age':3,'home':'three'},{'name':'d','age':4,'home':'four'},{'name':'e','age':5,'home':'five'}];已知数组1和2,求写一种方法得到数组3的形式
2 回答
阿波罗的战车
TA贡献1862条经验 获得超6个赞
functiontrans(names,arr){letresult=[]for(leti=0,len=arr[0].length;iletobj={} names.forEach((name,index)=>{obj[name]=arr[index][i]})result.push(obj)}returnresult}vararr1=['name','age','home']vararr2=[['a','b','c','d','e'],[1,2,3,4,5],['one','two','three','four','five']]trans(arr1,arr2)
阿晨1998
TA贡献2037条经验 获得超6个赞
vararr1=['name','age','home']vararr2=[['a','b','c','d','e'],[1,2,3,4,5],['one','two','three','four','five']]arr2[0].map((item,index)=>{returnarr1.reduce((a,b,i)=>(a[b]=arr2[i][index],a),{})})
添加回答
举报
0/150
提交
取消