If you want a hashCode() function like Java's in JavaScript, that is yours:
Copyfunction hashCode(string){
var hash = 0;
for (var i = 0; i < string.length; i++) {
var code = string.charCodeAt(i);
hash = ((hash<<5)-hash)+code;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
That is the way of implementation in Java (bitwise operator).
Please note that hashCode could be positive and negative, and that's normal, see HashCode giving negative values. So, you could consider to use Math.abs() along with this function.
If you want a hashCode() function like Java's in JavaScript, that is yours:
Copyfunction hashCode(string){
var hash = 0;
for (var i = 0; i < string.length; i++) {
var code = string.charCodeAt(i);
hash = ((hash<<5)-hash)+code;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
That is the way of implementation in Java (bitwise operator).
Please note that hashCode could be positive and negative, and that's normal, see HashCode giving negative values. So, you could consider to use Math.abs() along with this function.
JavaScript objects can only use strings as keys (anything else is converted to a string).
You could, alternatively, maintain an array which indexes the objects in question, and use its index string as a reference to the object. Something like this:
Copyvar ObjectReference = [];
ObjectReference.push(obj);
set['ObjectReference.' + ObjectReference.indexOf(obj)] = true;
Obviously it's a little verbose, but you could write a couple of methods that handle it and get and set all willy nilly.
Edit:
Your guess is fact -- this is defined behaviour in JavaScript -- specifically a toString conversion occurs meaning that you can can define your own toString function on the object that will be used as the property name. - olliej
This brings up another interesting point; you can define a toString method on the objects you want to hash, and that can form their hash identifier.
How does one deterministically hash an object in javascript? I tried to look for the answer in their github repo, but I couldn't figure it out.
Converting objects to hash signatures in JavaScript
String.hashCode() is plenty unique
I think folks sometimes forget that hashcodes aren't intended to be 100% unique, just a first order approximation that's distributed well enough for hashtable buckets. True equality is why equals() exists.
It isn't as if a hashcode collision will cause a set to deduplicate your object or overwrite your value in a map. That's what equals is for.
More on reddit.comCreating a Hash from a String in JavaScript
Videos
» npm install js-hash-code
See this excerpt from vue-query:
So the key is based on an array, which can contain primitives and objects. And while the order of the array does matter, apparently the order of the object's keys do not matter.
I tried to read through their code on GitHub to figure out how they hash that key and how it guarantees that it ignores the order of keys in objects. But I couldn't figure it out. Any help here would be greatly appreciated. 🙏