js 两个数组匹配相同的数据处理?
let arr = [ { type: "name"},
{ type: "age"},
{ type: "money"},
]
let arr1 = [ {
name: "X",
age: "18",
gender: 1,
money: 100,
sex: 1
},
{
name: "XX",
age: "20",
gender: 1,
money: 200,
sex: 1
},
]
期望得到:根据arr数组里面的type的值 去取arr1数组中对应的属性里面的值、返回新的数组
麻烦各位大佬帮忙看看
let res = [ {
name: "X",
age: "18",
money: 100
},
{
name: "XX",
age: "20",
money: 200
}
]
回答:
源数据和 arr1 一样,肯定是 arr1.map 然后具体内容和 arr 一样,肯定是 arr.reduce 呗
arr1.map(v=>{ return arr.reduce((s, n)=>{
const type = n.type
s[type] = v[type]
return s
},{})
})
回答:
这样?
arr1.map(i => Object.fromEntries(arr.map(j => [j.type, i[j.type]])))
以上是 js 两个数组匹配相同的数据处理? 的全部内容, 来源链接: utcz.com/p/933169.html