Skip to content

Instantly share code, notes, and snippets.

@Nithanaroy
Created February 22, 2017 00:56
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 Nithanaroy/d62a120ac58d417ca9caed0ad7fe8eaf to your computer and use it in GitHub Desktop.
Save Nithanaroy/d62a120ac58d417ca9caed0ad7fe8eaf to your computer and use it in GitHub Desktop.
Deeply compare two JavaScript objects
function deepEqual ( a, b ) {
let equalCount = Object.keys( a ).length === Object.keys( b ).length;
if ( equalCount ){
for ( let key in a ){
if ( typeof a[key] === typeof b[key] ){
if ( typeof a[key] !== "object" ){
if ( a[key] !== b[key] ){
return false;
}
} else {
let subKeyEqual = deepEqual( a[key], b[key] );
if ( !subKeyEqual ){
return false;
}
}
} else {
return false;
}
}
} else {
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment