使用变量键访问JavaScript对象中的值 [英] Using variable keys to access values in JavaScript objects

查看:135
本文介绍了使用变量键访问JavaScript对象中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

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

对象数据:

{
    "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
    }]
}

问题:

alert(data.A5A50000 [0] .time); 正确显示2009-05-27 16:36:45。

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

alert(bsID ); 正确显示A5A50000。

alert(bsID); properly displays "A5A50000".

alert(data.bsID [0] .time); 报告data.bsID未定义。

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

alert(data [bsID] [0] .time); 报告数据[bsID]未定义。

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

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

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.

推荐答案

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

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

所以你真正想要的是:

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

编辑

不确定你做错了什么,但这在Firebug的控制台上对我有用:

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

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

这篇关于使用变量键访问JavaScript对象中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆