Skip to content

Instantly share code, notes, and snippets.

@nbubna
Last active June 21, 2021 18:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbubna/5570057 to your computer and use it in GitHub Desktop.
Save nbubna/5570057 to your computer and use it in GitHub Desktop.
Simple object diffing, outputs to console log
function diff(a, b) {
var diffs = {};
for (var k in a) {
if (!(k in b)) {
console.log('b is missing '+k, a[k]);
diffs[k] = "missing";
} else if (a[k] != b[k]) {
var av = a[k], bv = b[k];
try {
if (JSON.stringify(av) != JSON.stringify(bv)) {
console.log('different '+k, av, bv);
}
} catch (e) {
if (av+'' != bv+'') {
console.log('different '+k, av, bv);
}
}
diffs[k] = "different";
}
}
for (var k in b) {
if (!(k in a)) {
console.log('b has '+k, b[k]);
diffs[k] = "extra";
}
}
return diffs;
}
@tad-lispy
Copy link

Thanks for sharing. There is an error on line 16. The diffs is undefined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment