Skip to content

Instantly share code, notes, and snippets.

@Yimiprod
Last active September 21, 2023 12:10
Star You must be signed in to star a gist
Embed
What would you like to do?
Deep diff between two object, using lodash
/**
* This code is licensed under the terms of the MIT license
*
* 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);
}
@Andrei-Fogoros
Copy link

@Andrei-Fogoros didn't thinked about licence at the time i posted it, but since you're asking, it's a good occasion to put it under MIT License.

Great, thank you very much! :)

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