0

I have the following objects

defaultObject = {
  item1: ""
  item2: 2
  item3: 3
  item4: ""
}

newObject = {
  item1: 1
  item2: 4
  item3: 3
  item4: "something"
}

Output which I expect is as below

{
  item1: 1
  item2: 4
  item4: "something"
}

I tried Compare two Arrays with Objects and create new array with unmatched objects.

1 Answers1

0

This should do it:

oldo = {
  item1: "",item2: 2,
  item3: 3,item4: ""
}

newo = {
  item1: 1,item2: 4,
  item3: 3,item4: "something"
}
const res=Object.entries(newo).reduce((a,[k,v])=>(oldo[k]==v||(a[k]=v),a),{});
console.log(res);

The res object is made up of only those entries (properties) that are not found in the oldo object.

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43