Skip to content

Instantly share code, notes, and snippets.

@Bengejd
Created December 17, 2018 21:39
Show Gist options
  • Save Bengejd/61f9f502fd34e924091f07417707f448 to your computer and use it in GitHub Desktop.
Save Bengejd/61f9f502fd34e924091f07417707f448 to your computer and use it in GitHub Desktop.
Deep Diff between two objects, using Lodash - Supports nested arrays.
getDiff() {
function changes(object, base) {
let arrayIndexCounter = 0;
return _.transform(object, function (result, value, key) {
if (!_.isEqual(value, base[key])) {
let resultKey = _.isArray(base) ? arrayIndexCounter++ : key;
result[resultKey] = (_.isObject(value) && _.isObject(base[key])) ? changes(value, base[key]) : value;
console.log("Result: " + JSON.stringify(result));
}
});
}
return changes(compareObject, baseObject);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment