-
-
Save jordansaints/a18a5100fe61ab86bc2b to your computer and use it in GitHub Desktop.
JavaScript object difference using Underscore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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