2013-07-22 57 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); 
}); 
相關問題