Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 337
Sanjeev Satheesh Avatar asked Mar 02 '11 19:03

Sanjeev Satheesh


People also ask

What is map Set in JavaScript?

The map. set() method is used to add key-value pairs to a Map object. It can also be used to update the value of an existing key. Each value must have a unique key so that they get mapped correctly.

What is difference between map and Set in JavaScript?

The difference between Map and Set. A Set is a collection dataset that needs to be composed of unique values, where a Map is when you have pairs of associated data when we map the keys to the value. Map and Set both have similar methods; these include .has(), . get(), .

Does map Set overwrite?

set() overwrites the value if the key already exists in the map. How can I test this easily when in a big project? Are there any website you can use for javascript coding? what should happen instead?

Is there a Set in JavaScript?

A JavaScript Set is a collection of unique values. Each value can only occur once in a Set. A Set can hold any value of any data type.


2 Answers

var box = { 
  length: 0,
  add: function(k, v) {
    if (typeof this[k] === 'undefined')
      this.length++;
    this[k] = v;
  }
}
like image 52
Eric Mickelsen Avatar answered Sep 20 '22 07:09

Eric Mickelsen


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
like image 28
Wayne Avatar answered Sep 20 '22 07:09

Wayne