Created
August 31, 2017 07:28
-
-
Save venik/4bd2b646c2906693c3c9cb6a032a6d1c to your computer and use it in GitHub Desktop.
Deep diff between two typescript objects with lodash, returns the difference
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
difference(newObject: any, baseObject: any) { | |
function changes(newObject: any, baseObject: any) { | |
return _.transform(newObject, function(result, value, key) { | |
if (!_.isEqual(value, baseObject[key])) { | |
result[key] = (_.isObject(value) && _.isObject(baseObject[key])) ? changes(value, baseObject[key]) : value; | |
} | |
}); | |
} | |
const rval = changes(newObject, baseObject); | |
let lval = changes(baseObject, newObject); | |
return _.assign(lval, rval); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this one is also good
https://gist.github.com/Yimiprod/7ee176597fef230d1451