Skip to content

Instantly share code, notes, and snippets.

@sethdavis512
Created October 11, 2018 17:01
Show Gist options
  • Save sethdavis512/58fef8dc861d5c1fa6216300b723a324 to your computer and use it in GitHub Desktop.
Save sethdavis512/58fef8dc861d5c1fa6216300b723a324 to your computer and use it in GitHub Desktop.
Lodash Diff Objects
import { transform, isEqual, isObject } from '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) {
return transform(object, (result, value, key) => {
if (!isEqual(value, base[key])) {
result[key] = isObject(value) && isObject(base[key]) ? difference(value, base[key]) : value;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment