数组合并方法

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的形式
一只名叫tom的猫
浏览 333回答 2
2回答

阿波罗的战车

functiontrans(names,arr){letresult=[]for(leti=0,len=arr[0].length;i{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

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),{})})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript