开发者

Simulating map/set in Javascript

I have a json object, say box = {}; to which I will keep adding key-values like box['somename'] = somevalue. There may be repetitions of somename and I want the last instance's value to win. All this is fine.

Now I need to operate on it, as if it were an array. Basically, now开发者_如何学Go that I have a set of unique keys, I want one main operation box.length to see how many unique elements there are. Is there an elegant constant time way of doing it without iterating through all properties of this object?


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


Increment a counter every time you add a new element to 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. Usage:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜