let arra = {
'aaa':'',
'bbb':'',
'ccc':''
}
let arrb = {
'aaa':'111',
'bbb':'222',
'ccc':'333',
'ddd':'444',
'eee':'555'
}
我需要arra里這些項(xiàng)的值保礼,實(shí)際后端會(huì)返回更多忙灼,類似于arrb
如何使用es6解構(gòu)賦值,將對(duì)應(yīng)的值賦給arra
({aaa: arra.aaa, bbb: arra.bbb, ccc: arra.ccc} = arrb);
其他方法(Object.fromEntries是es10的庇勃,謹(jǐn)慎使用)
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]});