Simulate a map / set in Javascript

I have a json object, say box = {}; to which I will add key values, such as box['somename'] = somevalue . There may be repetitions of somename , and I want to get the value of the last instance. All this is wonderful.

Now I need to operate as if it were an array. Basically, now that I have a set of unique keys, I need one basic box.length operation to see how many unique elements there are. Is there an elegant permanent way to do this without repeating all the properties of this object?

+7
source share
2 answers
 var box = { length: 0, add: function(k, v) { if (typeof this[k] === 'undefined') this.length++; this[k] = v; } } 
+9
source

Increase the counter every time you add a new item to the box .

 function Box() { var length = 0; var items = {}; this.add = function(k, v) { if (!(k in items)) length++; // don't count twice items[k] = v; } this.get = function(k) { return items[k]; } this.delete = function(k) { if (k in items) length--; delete items[k]; } this.__defineGetter__("length", function() { return length; }); } 

This version correctly handles adding and removing elements with any name and provides read-only access to the length property. Using:

 var box = new Box(); box.add("a", 1); box.add("a", 2); // overwrite box.add("b", "whatever"); box.add(null, 3); box.add(undefined, 3); box.add(undefined, 42); box.add("", 41); console.log(box.length); // 5 console.log(box.get(undefined)); // 42 console.log(box.get(null)); // 3 console.log(box.get("")); // 41 box.delete(undefined); box.delete(undefined); box.delete(undefined); box.delete(undefined); box.delete(undefined); box.delete(undefined); box.delete(22); // never was defined console.log(box.length); // 4 console.log(box.get(undefined)); // undefined box.add("length", "33") box.add("items", "jfhsdjkfh"); box.add("length", 77); console.log(box.length); // 6 
+4
source

All Articles