如何比较两个对象并返回不同的对象。
不同的元素被secondObject覆盖。
我想使用EcmaScript6
const firtObject = {a:"abc", b:"bcd", c:"cde"}
const secondObject = {b:"fff", c:"cde"}
const returnObject= {a:"abc", b:"fff", c:"cde"}
答案 0 :(得分:0)
使用简单的传播方式:
const firstObject = {a:"abc", b:"bcd", c:"cde"}
const secondObject = {b:"fff", c:"cde"};
const returnObject = { ...firstObject, ...secondObject };
console.log(returnObject);
.as-console-wrapper { max-height: 100% !important; top: auto; }
您也可以使用Object.assign
:
const firstObject = {a:"abc", b:"bcd", c:"cde"}
const secondObject = {b:"fff", c:"cde"};
const returnObject = Object.assign({}, firstObject, secondObject);
console.log(returnObject);
.as-console-wrapper { max-height: 100% !important; top: auto; }