Skip to content

Instantly share code, notes, and snippets.

@ryan-haskell
Created March 2, 2018 18:37
Show Gist options
  • Save ryan-haskell/9846e88921246e4b951b9806c2ee0608 to your computer and use it in GitHub Desktop.
Save ryan-haskell/9846e88921246e4b951b9806c2ee0608 to your computer and use it in GitHub Desktop.
Check deep equality in javascript
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