Skip to content Skip to sidebar Skip to footer

Creating A .net Like Dictionary Object In Javascript

I want to create a object in JavaScript which will store values in key, value pair and I should be able to pass some key and should be able to get its value back. In .NET world we

Solution 1:

Just use a standard javascript object:

var dictionary = {};//create new object
dictionary["key1"] = value1;//set key1var key1 = dictionary["key1"];//get key1

NOTE: You can also get/set any "keys" you create using dot notation (i.e. dictionary.key1)


You could take that further if you wanted specific functions for it...

functionDictionary(){
   var dictionary = {};

   this.setData = function(key, val) { dictionary[key] = val; }
   this.getData = function(key) { return dictionary[key]; }
}

var dictionary = newDictionary();
dictionary.setData("key1", "value1");
var key1 = dictionary.getData("key1");

Solution 2:

How about this class taken from Marijn Havereke's book Eloquent JavaScript

The fiddle

functionDictionary(values) {
    this.values = values || {};

    var forEachIn = function (object, action) {
      for (var property in object) {
        if (Object.prototype.hasOwnProperty.call(object, property))
          action(property, object[property]);
      }
    };

    Dictionary.prototype.containsKey = function(key) {
      returnObject.prototype.hasOwnProperty.call(this.values, key) &&
        Object.prototype.propertyIsEnumerable.call(this.values, key);
    };

    Dictionary.prototype.forEach = function(action) {
      forEachIn(this.values, action);
    };

    Dictionary.prototype.lookup = function(key) {
      returnthis.values[key];
    };

    Dictionary.prototype.add = function(key, value) {
      this.values[key] = value;
    };
};

var numberDic = newDictionary({One: 1,Two: 2, Three: 3});

//-- does key existconsole.log(numberDic.containsKey("One"));
console.log(numberDic.containsKey("One"));
console.log(numberDic.containsKey("Four"));

//-- loop through each item in the dic
numberDic.forEach(function(key, value) {
  console.log(key, "is", value);
});

//-- works with complex objects//------------------------------------------var complexObjectDic = newDictionary({
    Microsoft: {
        Something: "Real Interesting",
        About: "Microsoft",
        Will: "Go",
        Here: ".",
        ProductPrices: {
            WindowsPhone: 55.88,
            Windows :{
                WinXp : 180.00,
                Win7 : 200.00,
                Win8 : 150.00
            }
        }
    },
    Apple: {
        Did: "you",
        Hear: "the",
        New: "iphone",
        Will: "be coming out soon",
    }});

//-- does key existconsole.log(complexObjectDic.containsKey("Microsoft"));
console.log(complexObjectDic.containsKey("Apple"));
console.log(complexObjectDic.containsKey("Facebook"));

//-- search the dic by keyconsole.log(complexObjectDic.lookup("Microsoft"));
console.log(complexObjectDic.lookup("Apple"));

//-- add item to dic
complexObjectDic.add("Instagram", {
    This: "is",
    Another: "object",
    That: "I willl be Adding"
});

//-- loop through each item in the dic
complexObjectDic.forEach(function(key, value) {
    console.log(key, value);
});

Solution 3:

var widget={};
var key='k';
widget[key]='v';
alert(widget.k);//gives you v

Post a Comment for "Creating A .net Like Dictionary Object In Javascript"