2013-07-22 25 views
17

Voglio creare un oggetto in JavaScript che memorizzerà i valori in chiave, coppia di valori e dovrei essere in grado di passare qualche chiave e dovrebbe essere in grado di recuperare il suo valore. Nel mondo .NET possiamo usare la classe dizionario per questo tipo di implementazione. Abbiamo qualche opzione nel mondo JavaScript? Sto usando ExtJs 4.1, quindi se si conosce qualsiasi opzione in ExtJS, anche quella funzionerebbe.Creazione di un oggetto dizionario .net like in Javascript

Ho provato qualcosa di simile ma non riesco a ottenere valore tramite chiave.

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

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

risposta

30

basta usare un normale JavaScript Object:

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

NOTA: È inoltre possibile ottenere/impostare qualsiasi "chiavi" si creano usando la notazione punto (cioè dictionary.key1)


Potresti continuare a farlo se volessi funzioni specifiche ...

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

Invece {}, uso: dizionario var = Object.create (null); In questo modo si ottiene un oggetto senza prototipo. – neiker

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

Che ne dite di questa classe preso da 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); 
}); 
Problemi correlati