Last active
August 17, 2017 09:49
-
-
Save A1rPun/c739b0bfe1450efbc50e67668942ce19 to your computer and use it in GitHub Desktop.
Javascript value equality
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 getHashCode(obj) { | |
var hashCode = 0; | |
if (typeof obj === 'object') | |
for (var prop in obj) { | |
var code = getHashCode(prop) + getHashCode(obj[prop]); | |
hashCode += code * code; | |
} | |
else | |
for(var str = "" + obj, i = str.length; i--;) | |
hashCode += 100 / (i + str.charCodeAt(i)); | |
return hashCode; | |
} |
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 someObj = getHashCode({ id: 1, name: 'Pooka', isCat: true }); // 166.3732793411292 | |
var sameObj = getHashCode({ isCat: true, name: 'Pooka', id: 1 }); // 166.3732793411292 | |
if (someObj === sameObj) // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment