2013-07-22 5 views
17

JavaScript에서 키, 값 쌍의 값을 저장하는 객체를 만들고 싶습니다. 일부 키를 전달할 수 있어야하며 그 값을 다시 가져올 수 있어야합니다. .NET 세계에서는 사전 구현 클래스를 사용할 수 있습니다. JavaScript 세계에서 어떤 옵션이 있습니까? ExtJs 4.1을 사용하고 있습니다. ExtJS의 옵션 중 어떤 것이 작동하는지조차 알고 있다면 말이죠.자바에서 닷넷과 같은 사전 객체 만들기

나는 이런 식으로 시도했지만 열쇠로 가치를 얻을 수는 없습니다.

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

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

답변

30

는 그냥 표준을 사용하는 자바 스크립트 객체 :

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); 
});