Last active
July 13, 2018 18:03
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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