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

1 Answer 1

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');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.