Created
October 4, 2019 16:18
-
-
Save bittu/d97c7be9ebf8f307889e1b4dee426444 to your computer and use it in GitHub Desktop.
Simple deep object comparision
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
var equalityCheck = function(a, b) { | |
if (typeof a === 'object' && typeof b === 'object') { | |
if (!compareObjects(a, b)) { | |
return false; | |
} | |
} else if (a !== b) { | |
return false; | |
} | |
return true; | |
}; | |
var compareObjects = function(a, b) { | |
var className = toString.call(a); | |
if (className !== toString.call(b)) { | |
return false; | |
} | |
if (toString.call(a) === '[object Array]') { | |
var length = a.length | |
if (length !== b.length) { | |
return false; | |
} | |
while(length--) { | |
if (!equalityCheck(a[length], b[length])) { | |
return false; | |
} | |
} | |
} else if (typeof a === 'object') { | |
var currentkeys = Object.keys(a); | |
var nextKeys = Object.keys(b) | |
var length = currentkeys.length; | |
if (nextKeys.length !== length) { | |
return false; | |
} | |
while(length--) { | |
var key = currentkeys[length]; | |
if (nextKeys.indexOf(key) === -1) { | |
return false; | |
} | |
if (!equalityCheck(a[key], b[key])) { | |
return false; | |
} | |
} | |
} else if (a !== b) { | |
return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment