let arra = {
'aaa':'',
'bbb':'',
'ccc':''
}
let arrb = {
'aaa':'111',
'bbb':'222',
'ccc':'333',
'ddd':'444',
'eee':'555'
}
我需要arra里这些项的值,实际后端会返回更多,类似于arrb
如何使用es6解构赋值,将对应的值赋给arra
({aaa: arra.aaa, bbb: arra.bbb, ccc: arra.ccc} = arrb);
其他方法(Object.fromEntries是es10的,谨慎使用)
const result = (arrA, arrB) => Object.fromEntries(
Object.keys(arra)
.map(key => [key, arrb[key]])
)
const listAssign = (arrA, arrB) => Object.keys(arrA).forEach(key => { arrA[key] = arrB[key] || arrA[key]});