Created
February 22, 2017 00:56
-
-
Save Nithanaroy/d62a120ac58d417ca9caed0ad7fe8eaf to your computer and use it in GitHub Desktop.
Deeply compare two JavaScript objects
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 ( 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