Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a hashmap library for JavaScript?
    primarykey
    data
    text
    <p>In JavaScript, all Objects act a bit like hashmaps. However, the keys to these hashmaps must be strings. If they're not, they're converted with <code>toString()</code>. That means:</p> <pre><code>var a = {foo: 1}; var b = {bar: 2}; var o = {}; o[a] = 100; o[b]; // 100 JSON.stringify(o); // '{"[object Object]":100}' </code></pre> <p>That is, since the <code>toString()</code> of any plain Object is <code>[object Object]</code>, they all address the same value.</p> <p>I'd like to create a hashmap where Objects with the same properties and values address the same value, but objects with different properties or values address different values. That is:</p> <pre><code>var a = {foo: 1}; var b = {bar: 2, baz: 3}; var c = {baz: 3, bar: 2}; var hash = new Hash(); hash.set(a, 100); hash.get(b); // undefined hash.set(b, 200); hash.get(b); // 200 hash.get(c); // 200 </code></pre> <p>My first instinct was to use <code>JSON.stringify()</code> to turn objects into strings, but:</p> <pre><code>var hash = {}; var b = {bar: 2, baz: 3}; var c = {baz: 3, bar: 2}; hash[JSON.stringify(b)] = 100 hash[JSON.stringify(b)] // 100 hash[JSON.stringify(c)] // undefined JSON.stringify(b) // '{"bar":2,"baz":3}' JSON.stringify(c) // '{"baz":3,"bar":2}' </code></pre> <p>That is, JSON serialization is order-dependent.</p> <p>Is there a good library or technique to implement a hashmap like this?</p> <p><strong>Update</strong>:</p> <p>Equivalently, is there a good hashing function such that:</p> <pre><code>hash({foo: 1, bar: 2}) == hash({bar: 2, foo: 1}) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload