var arr1=['name', 'age', 'home']var arr2=[['a', 'b', 'c', 'd', 'e'], [1,2,3,4,5], ['one', 'two', 'three', 'four', 'five']]var arr3=[{'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贡献1900条经验 获得超5个赞
function trans (names, arr) {
let result = []
for (let i = 0, len = arr[0].length; i < len; i++) {
let obj = {}
names.forEach((name, index) => {
obj[name] = arr[index][i]
})
result.push(obj)
}
return result
}
var arr1=['name', 'age', 'home']
var arr2=[['a', 'b', 'c', 'd', 'e'], [1,2,3,4,5], ['one', 'two', 'three', 'four', 'five']]
trans(arr1, arr2)
牛魔王的故事
TA贡献1830条经验 获得超3个赞
var arr1=['name', 'age', 'home']
var arr2=[['a', 'b', 'c', 'd', 'e'], [1,2,3,4,5], ['one', 'two', 'three', 'four', 'five']]
arr2[0].map((item, index) => {
return arr1.reduce((a, b, i) => (a[b] = arr2[i][index], a), {})
})
添加回答
举报
0/150
提交
取消