Created
December 20, 2014 06:49
-
-
Save wcang/cbd1d02ec97f8f1f12ec to your computer and use it in GitHub Desktop.
Deep comparison in Javascript
This file contains 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 deepEqual(obj1, obj2) | |
{ | |
if (obj1 === obj2) { | |
return true; | |
} | |
if (typeof(obj1) == typeof(obj2)) { | |
if (typeof(obj1) == 'object') { | |
var obj1Prop = 0; | |
var obj2Prop = 0; | |
for (var prop in obj1) { | |
obj1Prop++; | |
} | |
for (var prop in obj2) { | |
obj2Prop++; | |
} | |
if (obj1Prop != obj2Prop) | |
return false; | |
for (var prop in obj1) { | |
if (prop in obj2) { | |
if (!deepEqual(obj1[prop], obj2[prop])) | |
return false; | |
} | |
else { | |
return false; | |
} | |
} | |
return true; | |
} | |
else { | |
return (isNaN(obj1) && isNaN(obj2)) || obj1 === obj2; | |
} | |
} | |
else { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment