1

How to compare two objects and return an object that differs.

Elements that differ are overwritten by the secondObject.

I want to use EcmaScript6

const firtObject = {a:"abc", b:"bcd", c:"cde"}
const secondObject = {b:"fff", c:"cde"}

const returnObject= {a:"abc", b:"fff", c:"cde"}
benxmario
  • 25
  • 1
  • 6

1 Answers1

0

Use simple spreading:

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; }

You could also use 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; }
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79