Skip to content

Instantly share code, notes, and snippets.

@imekachi
Forked from Yimiprod/difference.js
Last active June 8, 2018 08:10
Show Gist options
  • Save imekachi/db910f64bc5efb9a2299583b63078e62 to your computer and use it in GitHub Desktop.
Save imekachi/db910f64bc5efb9a2299583b63078e62 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) {
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