按字符串索引访问字典对象

Access Dictionary Object by String Index

本文关键字:字典 对象 访问 索引 字符串      更新时间:2023-09-26

我正在尝试填充字典(JavaScript对象)并使用字符串索引从中检索值。出于某种原因,当我尝试检索值时,它总是返回undefined

我的代码是这样的:

var _gauges = {};
//fill the gauges
_gauges[gaugeName] = gaugeObject;

然后我尝试按如下方式访问它:

    setValue: function (gaugeName, newValue) {
        var thisGauge = _gauges[gaugeName]; //always undefined
        console.log(_gauges); //output shows all the elements that were added to _gauges
        if (thisGauge) {
            thisGauge.setCell(0, 1, newValue);
        }
    }

我在这里做错了什么吗?

好吧,

这是由于拼写错误...传递给 setValue 方法的参数与用于将项添加到字典对象的参数不匹配。