Created
March 2, 2018 18:37
-
-
Save ryan-haskell/9846e88921246e4b951b9806c2ee0608 to your computer and use it in GitHub Desktop.
Check deep equality 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
const equals = (obj1, obj2) => { | |
const sortKeys = (obj) => | |
(obj && typeof obj === 'object') | |
? Object.keys(obj) | |
.sort((a, b) => a < b) | |
.reduce((newObj, key) => { | |
newObj[key] = sortKeys(obj[key]) | |
return newObj | |
}, {}) | |
: obj | |
const [ one, two ] = | |
[ obj1, obj2 ] | |
.map(sortKeys) | |
.map(JSON.stringify) | |
return one === two | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment