Created
February 26, 2024 21:54
-
-
Save neodigm/87f4109bff0c33dd496dde6432ffa26a to your computer and use it in GitHub Desktop.
Return the difference between two JavaScript objects (shallow compare)
This file contains 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
shallowDelta(newObj, oldObj){ | |
if (Object.keys(oldObj).length == 0 | |
&& Object.keys(newObj).length > 0) | |
return newObj; | |
let diff = {}; | |
for (const key in oldObj) { | |
if (newObj[key] && oldObj[key] != newObj[key] ) { | |
diff[key] = newObj[key]; | |
} | |
} | |
if (Object.keys(diff).length > 0) return diff; | |
return oldObj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment