Skip to content

Instantly share code, notes, and snippets.

@shamaru001
Forked from Yimiprod/difference.js
Created March 7, 2020 06:22
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 shamaru001/e4a1dabdd853789a337849db2dd4a145 to your computer and use it in GitHub Desktop.
Save shamaru001/e4a1dabdd853789a337849db2dd4a145 to your computer and use it in GitHub Desktop.
Deep diff between two object, using lodash
/**
* Deep diff between two object, using lodash
* @param {Object} object Object compared
* @param {Object} base Object to compare with
* @return {Object} Return a new object who represent the diff
*/
function difference(object, base) {
function changes(object, base) {
return _.transform(object, function(result, value, key) {
if (!_.isEqual(value, base[key])) {
result[key] = (_.isObject(value) && _.isObject(base[key])) ? changes(value, base[key]) : value;
}
});
}
return changes(object, base);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment