Skip to content

Instantly share code, notes, and snippets.

@vladPovalii
Forked from Yimiprod/difference.js
Last active November 16, 2018 15:42
Show Gist options
  • Save vladPovalii/13a21f4ebb84bc903dcb93de96290cd5 to your computer and use it in GitHub Desktop.
Save vladPovalii/13a21f4ebb84bc903dcb93de96290cd5 to your computer and use it in GitHub Desktop.
Deep diff between two object, using lodash
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