Skip to content

Instantly share code, notes, and snippets.

@lu1s
Created April 11, 2012 18:52
Show Gist options
  • Save lu1s/2361359 to your computer and use it in GitHub Desktop.
Save lu1s/2361359 to your computer and use it in GitHub Desktop.
javascript object equality
// y and x can be whatever you look for equality
function objeq(y,x)
{
var p;
for(p in y) {
if(typeof(x[p])=='undefined') {return false;}
}
for(p in y) {
if (y[p]) {
switch(typeof(y[p])) {
case 'object':
if (!objeq(y[p],x[p])) { return false; } break;
case 'function':
if (typeof(x[p])=='undefined' ||
(p != 'equals' && y[p].toString() != x[p].toString()))
return false;
break;
default:
if (y[p] != x[p]) { return false; }
}
} else {
if (x[p])
return false;
}
}
for(p in x) {
if(typeof(y[p])=='undefined') {return false;}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment