首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Javascript中创建一个类似.net的字典对象

在Javascript中创建一个类似.net的字典对象
EN

Stack Overflow用户
提问于 2013-07-22 20:36:41
回答 3查看 19.7K关注 0票数 17

我想在JavaScript中创建一个对象,它将值存储在键,值对中,我应该能够传递一些键,并且应该能够取回它的值。在.NET世界中,我们可以使用dictionary类来实现这一类。在JavaScript的世界里我们有什么选择吗?我使用的是ExtJs4.1,所以如果你知道ExtJs中的任何选项,那也可以。

我已经尝试过这样的方法,但我无法通过键获取值。

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

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

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-07-22 20:40:08

只需使用标准的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");
票数 30
EN

Stack Overflow用户

发布于 2014-08-02 08:40:59

这个取自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);
});
票数 3
EN

Stack Overflow用户

发布于 2013-07-22 20:50:06

var widget={};
var key='k';
widget[key]='v';
alert(widget.k);//gives you v
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17787754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档