使用变量键访问JavaScript对象中的值

时间:2023-01-05 21:38:02

The code:

代码:

function updateDashboardData() {
    $.getJSON("includes/system/ajaxDataInterface.php", {recordcount:1}, function(data) {
        $('.stationContainer').each(function(data) {
            var bsID = $(this).attr("id");
            var bsStatus = $(this).children('.stationStatus');
            alert(data[bsID][0].time);
            bsStatus.find('.bs_maxHandsets').text(data[bsID][0].maxHandsets);
            bsStatus.find('.bs_time').text(data[bsID][0].time);
        });
    });
}

The object data:

对象数据:

{
    "A5A50000": [{
        "bsid": "A5A50000",
        "chanCount": 17,
        "time": "2009-05-27 16:36:45",
        "avgInterference": 1.711765,
        "maxInterference": 4.97,
        "avgHandsets": 205.1176,
        "maxHandsets": 315,
        "avgCalls": 6.4118,
        "maxCalls": 13,
        "avgCBA": 3868.98059,
        "maxCBA": 7463,
        "sumSuccessCBA": 197318,
        "sumTimeoutHandoff": 133,
        "sumAttemptHandoff": 1028,
        "sumDeniedHandoff": 216,
        "sumConfirmHandoff": 679,
        "sumHandoffNetwork": 61873,
        "sumJoinNetwork": 96888,
        "sumLeaveNetwork": 93754,
        "sumRcvdKeepalive": 98773,
        "sumTimeoutKeepalive": 19748,
        "sumAttemptUplink": 93689,
        "sumBlockedUplink": 62453
    }]
}

The problem:

问题:

alert(data.A5A50000[0].time); properly displays "2009-05-27 16:36:45".

警报(data.A5A50000 [0]。时间);正确显示“2009-05-27 16:36:45”。

alert(bsID); properly displays "A5A50000".

警报(BSID);正确显示“A5A50000”。

alert(data.bsID[0].time); reports "data.bsID is undefined".

警报(data.bsID [0]。时间);报告“data.bsID未定义”。

alert(data[bsID][0].time); reports "data[bsID] is undefined".

警报(数据[BSID] [0]。时间);报告“数据[bsID]未定义”。

I'm a little unclear when a variable is/isn't evaluated. Maybe I'm overlooking something silly, but I can't figure out my problem here.

我有点不清楚变量是否被评估。也许我忽视了一些愚蠢的事情,但我无法弄清楚我的问题。

2 个解决方案

#1


102  

You can access object properties by dot notation or by bracket notation.

您可以通过点表示法或括号表示法访问对象属性。

var x = {'test': 'hi'};
alert(x.test); // alerts hi
alert(x['test']); // alerts hi

When you have a dynamic value, you have to use the latter:

如果您有动态值,则必须使用后者:

var property = 'test';
alert(x.property); // looks for x.property, undefined if it doesn't exist
alert(x[property]); // looks for x['test'], alerts hi

So what you actually want is:

所以你真正想要的是:

alert(data[bsID][0].time);

EDIT:

编辑:

Not sure what you're doing wrong, but this is working for me on Firebug's console:

不确定你做错了什么,但这对Firebug的控制台起作用了:

var data = {"A5A50000":[{"bsid":"A5A50000","chanCount":17,"time":"2009-05-27 16:36:45","avgInterference":1.711765,"maxInterference":4.97,"avgHandsets":205.1176,"maxHandsets":315,"avgCalls":6.4118,"maxCalls":13,"avgCBA":3868.98059,"maxCBA":7463,"sumSuccessCBA":197318,"sumTimeoutHandoff":133,"sumAttemptHandoff":1028,"sumDeniedHandoff":216,"sumConfirmHandoff":679,"sumHandoffNetwork":61873,"sumJoinNetwork":96888,"sumLeaveNetwork":93754,"sumRcvdKeepalive":98773,"sumTimeoutKeepalive":19748,"sumAttemptUplink":93689,"sumBlockedUplink":62453}]};
var bsID = 'A5A50000';
alert(data[bsID][0].time);

#2


3  

In Javascript, you can use either object or array-style notation to look up an attribute. The following are equivalent:

在Javascript中,您可以使用对象或数组样式表示法来查找属性。以下是等效的:

data.A5A50000
data['A5A50000']

With the second syntax, you can use a variable in place of an object string:

使用第二种语法,您可以使用变量代替对象字符串:

data[bsID][0]

#1


102  

You can access object properties by dot notation or by bracket notation.

您可以通过点表示法或括号表示法访问对象属性。

var x = {'test': 'hi'};
alert(x.test); // alerts hi
alert(x['test']); // alerts hi

When you have a dynamic value, you have to use the latter:

如果您有动态值,则必须使用后者:

var property = 'test';
alert(x.property); // looks for x.property, undefined if it doesn't exist
alert(x[property]); // looks for x['test'], alerts hi

So what you actually want is:

所以你真正想要的是:

alert(data[bsID][0].time);

EDIT:

编辑:

Not sure what you're doing wrong, but this is working for me on Firebug's console:

不确定你做错了什么,但这对Firebug的控制台起作用了:

var data = {"A5A50000":[{"bsid":"A5A50000","chanCount":17,"time":"2009-05-27 16:36:45","avgInterference":1.711765,"maxInterference":4.97,"avgHandsets":205.1176,"maxHandsets":315,"avgCalls":6.4118,"maxCalls":13,"avgCBA":3868.98059,"maxCBA":7463,"sumSuccessCBA":197318,"sumTimeoutHandoff":133,"sumAttemptHandoff":1028,"sumDeniedHandoff":216,"sumConfirmHandoff":679,"sumHandoffNetwork":61873,"sumJoinNetwork":96888,"sumLeaveNetwork":93754,"sumRcvdKeepalive":98773,"sumTimeoutKeepalive":19748,"sumAttemptUplink":93689,"sumBlockedUplink":62453}]};
var bsID = 'A5A50000';
alert(data[bsID][0].time);

#2


3  

In Javascript, you can use either object or array-style notation to look up an attribute. The following are equivalent:

在Javascript中,您可以使用对象或数组样式表示法来查找属性。以下是等效的:

data.A5A50000
data['A5A50000']

With the second syntax, you can use a variable in place of an object string:

使用第二种语法,您可以使用变量代替对象字符串:

data[bsID][0]