两个数组,A数组里对象的值,是B数组里对象的key

问题描述

let arr1 = [
  {abc:"name"},
  {abc:"content"}
]
let arr2 = [
  {0:"111",1:"222"},
  {0:"333",1:"444"}
]

你期待的结果是什么?

let arr3 = [
  {name:"111",content:"222"},
  {name:"333",content:"444"}
]
阅读 2.4k
2 个回答

let arr3 = []
arr2.map((item,index) => {

for(var i in item){
    let key = arr1[i].abc
    arr3[index] = {
        ...arr3[index],
        [key]:item[i]
    }
}

})

图片描述

const arr3 = arr2.map(i=>{
  const obj = {}
  for(var j in i){
    obj[arr1[j].abc]=i[j]
  }
  return obj
})
推荐问题
宣传栏