Cmag
Cmag

Reputation: 15770

JavaScript comparing and returning differences between Objects

Folks, Given the following 2 Objects, what would be the best way to compare them, and return only the differences between them?

{ _id: 530797d8952e10129f97fde3,
  Product: 'something',
  Realm: [ 'something', 'something','foo' ],
  Service: 'Node',
  Owners: [ 'foo', 'something' ],
  Project: 'something',
  URLs:
    [ { 'foo': [Object] },
      { 'foo': [Object] },
      { 'foo': [Object] } ] }

...

{ _id: 530797d8952e10129f97fde3,
  Product: 'something',
  Realm: [ 'something', 'something','foo' ],
  Service: 'Node',
  Owners: [ 'foo', 'something' ],
  Project: 'something',
  URLs:
    [ { 'bar': [Object] },
      { 'foo': [Object] },
      { 'quux': [Object] } ] }

Should I loop through these and compare, or is there a pre-built module people use?

Thanks!

Upvotes: 0

Views: 982

Answers (1)

J David Smith
J David Smith

Reputation: 4820

There is a fantastic node.js module written by flitbit that will do exactly what you desire: deep-diff.

I won't go into to much detail about it (the module page is well-written), but it will compute the difference between two objects and given you a detailed output describing it. The output includes whether a property is new/deleted or merely has a different value.

Upvotes: 1

Related Questions