Skip to content

Instantly share code, notes, and snippets.

@kevinmarks
Created February 14, 2024 22:40
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 kevinmarks/093db5313ab7f0fa479bd823df2c1128 to your computer and use it in GitHub Desktop.
Save kevinmarks/093db5313ab7f0fa479bd823df2c1128 to your computer and use it in GitHub Desktop.
Diffing two objects in js - checks that the new has at least the same keys as the old, and if not, shows where.
function diffdata(legacy,shiny) {
let delta={}
for (const key in legacy){
const oldval = legacy[key];
const newval = shiny[key];
if (typeof(oldval)==='object' && typeof(newval)==='object'){
delta[key] = diffdata(oldval,newval)
} else if (oldval==newval){
delta[key]="✅";
} else if(Math.abs((oldval-newval)/oldval)<0.001){
delta[key]="≅";
} else {
delta[key]="❌ expected: "+oldval+" got: "+newval;
}
}
return delta;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment