Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a naive implementation I just put together - as keparo mentioned in a comment, one of the big issues is equality checking:</p> <pre><code>var ObjectMap = function() { this._keys = []; this._values = []; }; ObjectMap.prototype.clear = function() { this._keys = []; this._values = []; }; ObjectMap.prototype.get = function(key) { var index = this._indexOf(key, this._keys); if (index != -1) { return this._values[index]; } return undefined; }; ObjectMap.prototype.hasKey = function(key) { return (this._indexOf(key, this._keys) != -1); }; ObjectMap.prototype.hasValue = function(value) { return (this._indexOf(value, this._values) != -1); }; ObjectMap.prototype.put = function(key, value) { var index = this._indexOf(key, this._keys); if (index == -1) { index = this._keys.length; } this._keys[index] = key; this._values[index] = value; }; ObjectMap.prototype.remove = function(key) { var index = this._indexOf(key, this._keys); if (index != -1) { this._keys.splice(index, 1); this._values.splice(index, 1); } }; ObjectMap.prototype.size = function() { return this._keys.length; }; ObjectMap.prototype._indexOf = function(item, list) { for (var i = 0, l = list.length; i &lt; l; i++) { if (this._equals(list[i], item)) { return i; } } return -1; }; ObjectMap.prototype._equals = function(a, b) { if (a === b) { return true; } // Custom objects can implement an equals method if (typeof a.equals == "function" &amp;&amp; typeof b.equals == "function") { return a.equals(b); } // Arrays are equal if they're the same length and their contents are equal if (a instanceof Array &amp;&amp; b instanceof Array) { if (a.length != b.length) { return false; } for (var i = 0, l = a.length; i &lt; l; i++) { if (!this._equals(a[i], b[i])) { return false; } } return true; } // Checking object properties - objects are equal if they have all the same // properties and they're all equal. var seenProperties = {}; for (var prop in a) { if (a.hasOwnProperty(prop)) { if (!b.hasOwnProperty(prop)) { return false; } if (!this._equals(a[prop], b[prop])) { return false; } seenProperties[prop] = true; } } for (var prop in b) { if (!(prop in seenProperties) &amp;&amp; b.hasOwnProperty(prop)) { if (!a.hasOwnProperty(prop)) { return false; } if (!this._equals(b[prop], a[prop])) { return false; } } } return true; }; </code></pre> <p>Example usage:</p> <pre><code>&gt;&gt;&gt; var map = new ObjectMap(); &gt;&gt;&gt; var o = {a: 1, b: [1,2], c: true}; &gt;&gt;&gt; map.put(o, "buns"); &gt;&gt;&gt; map.get(o) "buns" &gt;&gt;&gt; map.get({a: 1, b: [1,2], c: true}); "buns" &gt;&gt;&gt; map.get({a: 1, b: [1,2], c: true, d:"hi"}); &gt;&gt;&gt; var a = [1,2,3]; &gt;&gt;&gt; map.put(a, "cheese"); &gt;&gt;&gt; map.get(a); "cheese" &gt;&gt;&gt; map.get([1,2,3]); "cheese" &gt;&gt;&gt; map.get([1,2,3,4]); &gt;&gt;&gt; var d = new Date(); &gt;&gt;&gt; map.put(d, "toast"); &gt;&gt;&gt; map.get(d); "toast" &gt;&gt;&gt; map.get(new Date(d.valueOf())); "toast" </code></pre> <p>This is in no way a complete implementation, just a pointer for a way to implement such an object. For example, looking at what I've given, you would also need to add constructor property checks before the object property check, as this currently works:</p> <pre><code>&gt;&gt;&gt; function TestObject(a) { this.a = a; }; &gt;&gt;&gt; var t = new TestObject("sandwich"); &gt;&gt;&gt; map.put(t, "butter"); &gt;&gt;&gt; map.get({a: "sandwich"}) "butter" </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.
    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