Skip to content

Instantly share code, notes, and snippets.

@zackthehuman
Created June 12, 2013 00:59
Show Gist options
  • Save zackthehuman/5762171 to your computer and use it in GitHub Desktop.
Save zackthehuman/5762171 to your computer and use it in GitHub Desktop.
JavaScript object difference using Underscore
var objectA = { a: '1a', b: '1b', c: '1c' };
var objectB = { a: '2a', b: '2b', d: '2d' };
function objectDifference(minuend, subtrahend) {
var firstIntersection = _.omit(minuend, _.keys(subtrahend)),
secondIntersection = _.omit(subtrahend, _.keys(minuend));
return _.extend(firstIntersection, secondIntersection);
}
var objectC = objectDifference(objectA, objectB);
console.log(objectC); // {c:"1c", d:"2d"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment