1

I have this JSON:

{ 
    "USD" : {"15m" : 559.07, "last" : 559.07, "buy" : 559.07, "sell" : 562.39,  "symbol" : "$"},
    "CNY" : {"15m" : 3431.69912796, "last" : 3431.69912796, "buy" : 3431.69912796, "sell" : 3452.0780449199997,  "symbol" : "¥"}
}

Using nodejs, I've been trying to iterate through and return any of the members of the nested object. Assuming I wanted to get the member "last", I tried the following. However, I get "undefined". How should I access these members correctly?

        var bcData = JSON.parse(body);
        for (var key in bcData) {
            console.log(key + ": " + key.last + '\n');
        }
Hossain Muctadir
  • 3,546
  • 1
  • 19
  • 33
Ci3
  • 4,632
  • 10
  • 34
  • 44
  • possible duplicate of [How do I enumerate the properties of a javascript object?](http://stackoverflow.com/questions/85992/how-do-i-enumerate-the-properties-of-a-javascript-object) – Felix Kling Mar 02 '14 at 06:47

1 Answers1

3

The object is still bcData. You need to first access key of bcData and then it's last property -

for (var key in bcData) {
    console.log(key + ": " + bcData[key].last + '\n');
}
Mukesh Soni
  • 6,646
  • 3
  • 30
  • 37