Skip to content

Instantly share code, notes, and snippets.

@neodigm
Created February 26, 2024 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neodigm/87f4109bff0c33dd496dde6432ffa26a to your computer and use it in GitHub Desktop.
Save neodigm/87f4109bff0c33dd496dde6432ffa26a to your computer and use it in GitHub Desktop.
Return the difference between two JavaScript objects (shallow compare)
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