Last active
December 18, 2015 12:19
-
-
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.
This file contains hidden or 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
// 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