Skip to content

Instantly share code, notes, and snippets.

@joroshiba
Last active July 13, 2018 18:03
Show Gist options
  • Save joroshiba/82035c790c91dcf92d7f5721fb750e90 to your computer and use it in GitHub Desktop.
Save joroshiba/82035c790c91dcf92d7f5721fb750e90 to your computer and use it in GitHub Desktop.
JS function which takes a new 'object' and the 'currentObj' and returns an object telling you which values have changed on from the currentObj to the object
function getDiff (object, currentObj)) {
let diff = {};
for (let key in object) {
if (!object.hasOwnProperty(key)) continue;
if (typeof object[key] === 'object' && object[key] && typeof currentObj[key] === 'object' && currentObj[key]) {
diff[key] = this.getDiff(object[key], currentObj[key]);
} else if (object[key] !== currentObj[key]) {
diff[key] = object[key];
}
}
for (let key in currentObj) {
if (!currentObj.hasOwnProperty(key)) continue;
if (currentObj[key] && object[key] === undefined) {
diff[key] = undefined;
}
}
return diff;
};
export default getDiff;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment