Skip to content

Instantly share code, notes, and snippets.

@niksumeiko
Last active December 18, 2015 12:19
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 niksumeiko/5781849 to your computer and use it in GitHub Desktop.
Save niksumeiko/5781849 to your computer and use it in GitHub Desktop.
JavaScript function that I am using to compare 2 JavaScript object without taking in mind prototypes of these objects.
// Function that compares 2 objects
function equals(obj1, obj2, skip) {
var i, l;
if ( typeof obj1 === "object" && typeof obj2 === "object") {
obj1 = JSON.stringify(obj1);
obj2 = JSON.stringify(obj2);
if (skip && skip.length) {
obj1 = JSON.parse(obj1);
obj2 = JSON.parse(obj2);
l = skip.length;
for (i = 0; i < l; i += 1) {
delete obj1[skip[i]];
delete obj2[skip[i]];
}
obj1 = JSON.stringify(obj1);
obj2 = JSON.stringify(obj2);
}
return obj1 === obj2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment