2013-07-22 29 views
17

我想在JavaScript中创建一个对象,它将值存储在键,值对中,我应该能够传递一些密钥,并且应该能够返回其值。在.NET世界中,我们可以使用字典类来实现这种实现。我们在JavaScript世界有什么选择吗?我使用的是ExtJs 4.1,所以如果你知道ExtJS中的任何选项,即使这样做也行。在Javascript中创建类似于.net的字典对象

我已经尝试过这样的事情,但我无法通过键获得价值。

var Widget = function(k, v) { 
    this.key = k; 
    this.value = v; 
}; 

var widgets = [ 
    new Widget(35, 312), 
    new Widget(52, 32) 
]; 

回答

30

只需使用一个标准的JavaScript对象:

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

注意:您还可以获取/设置任何的 “钥匙” 您创建使用点符号(即dictionary.key1


如果你想要它的特定功能,你可以进一步...

function Dictionary(){ 
    var dictionary = {}; 

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

var dictionary = new Dictionary(); 
dictionary.setData("key1", "value1"); 
var key1 = dictionary.getData("key1"); 
+6

而是采取{},使用: VAR字典=的Object.create(NULL); 这样,你会得到一个无原型的对象。 – neiker

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

这样如何类从Marijn Havereke's book Eloquent JavaScript

The fiddle

function Dictionary(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) { 
     return Object.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) { 
     return this.values[key]; 
    }; 

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

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

//-- does key exist 
console.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 = new Dictionary({ 
    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 exist 
console.log(complexObjectDic.containsKey("Microsoft")); 
console.log(complexObjectDic.containsKey("Apple")); 
console.log(complexObjectDic.containsKey("Facebook")); 

//-- search the dic by key 
console.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); 
}); 
相关问题