78

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".

alert(bsID); properly displays "A5A50000".

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

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.

JJJ
  • 32,902
  • 20
  • 89
  • 102
mikegreenberg
  • 1,411
  • 1
  • 12
  • 19
  • Uhm, I'm testing it too and it works for me... – Paolo Bergantino May 28 '09 at 19:22
  • What happens if you do alert(bsID);? is it defined? – Paolo Bergantino May 28 '09 at 19:31
  • 1
    alert(bsID) reports "A5A50000". Updated original updateDashboardData() function above. When I initiate the function from the console by updateDashboardData(); I am greeted with: "data[bsID] is undefined http://192.168.2.236/michaelg/js/xmonitor.js Line 21". Line 21 is the alert(). Is there anything functionally different between the initial code and how I'm applying it here which might be affecting the outcome? – mikegreenberg May 28 '09 at 19:34
  • Yes! Replace this function(data) { With this function() { in the inner each function. you're overriding the value. – Paolo Bergantino May 28 '09 at 19:44
  • Bingo! Not quite sure why I did that. :/ Thank you much! – mikegreenberg May 28 '09 at 19:52
  • No problem. Welcome to the site. :) – Paolo Bergantino May 28 '09 at 19:53
  • Does this answer your question? [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – shmuels Jun 04 '20 at 16:30
  • Using variable keys to access values in JavaScript objects in [typescript](https://stackoverflow.com/questions/41993515/access-object-key-using-variable-in-typescript/66838662#66838662) – tinystone Sep 05 '22 at 10:44

3 Answers3

192

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:

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);
Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
  • After attempting your suggestion, alert(data[bsID][0].time); reports "data[bsID] is undefined". I've updated the code I'm testing with above. I'm testing this within the console of Firebug (on FF, obviously). – mikegreenberg May 28 '09 at 19:19
  • I tried your test code and it, too, worked fine for me. Looking at the code above, the only thing I can imagine going wrong is the bsID is not what was expected. Testing the type of bsID, it reports "string". Further testing suggestions a problem with the data returned from getJSON(). When I attempt to access data[bsID][0] where data is from getJSON, it reports undefined. If I explicitly define data as the object I posted in the orig question, it works. I need to play with the returned data further to see what's wrong. – mikegreenberg May 28 '09 at 19:42
4

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

data.A5A50000
data['A5A50000']

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

data[bsID][0]
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

I experienced the same problem with a nested JSON API-response:

[
    {
        "bj_code": "2019",
        "BJ_PERIODE": [
            {
                "nummer": 1
            },
            {
                "nummer": 2
            }
        ]
    }
]   

I could evaluate:

pm.expect(pm.response.json()[0].BJ_PERIODE[1].nummer).to.eql(2);

But working with BJ_PERIODE and nummer through a variable didn't work. Writing it with the bracket method did work, even in this nested JSON, like this:

const periode = "BJ_PERIODE";
const nr = "nummer"; 
pm.expect(pm.response.json()[0][periode][1][nr]).to.eql(2);
NelemaniA
  • 11
  • 4