Skip to content

Instantly share code, notes, and snippets.

@venik
Created August 31, 2017 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save venik/4bd2b646c2906693c3c9cb6a032a6d1c to your computer and use it in GitHub Desktop.
Save venik/4bd2b646c2906693c3c9cb6a032a6d1c to your computer and use it in GitHub Desktop.
Deep diff between two typescript objects with lodash, returns the difference
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);
}
@venik
Copy link
Author

venik commented Sep 10, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment