1

I'm trying to access from an API the values of some currencies, and I'm not managing to get them from within the function.

The API returns after parsing this: {"ETH":{"USD":188.01},"BTC":{"USD":10330.41}}

This is the code I used:

fetch('https://min-api.cryptocompare.com/data/pricemulti?fsyms=ETH,BTC&tsyms=USD')
    .then(val => val.json()).then(data => {
        info = data; for (var item in data) { console.log(item.USD) }
    });

It logs undefined.

When I do in it console.log(item), it logs ETH and BTC as strings and not as objects as they should be. When I write in the console (outside of .then) info.ETH.USD, I get the results.

What did I get wrong?

2 Answers 2

2

The reason it was not working for you because you were looping through the object and returning the keys and not the actual object. item refers to keys of the object.

Try this:

fetch('https://min-api.cryptocompare.com/data/pricemulti?fsyms=ETH,BTC&tsyms=USD').
then(res => res.json())
.then(data => {
  console.log("The object", data)
  for(var item in data) {
    console.log(data[item])
  }
})

Now you can refer to the USD property as data[item].USD.

Sign up to request clarification or add additional context in comments.

3 Comments

Mark it as the correct answer if it helped you, will help future readers.
In JavaScript, you have to train yourself to think "key" after saying "for". Because for gives you keys, not values. It's for (key in It's not for (value in. This applies to both objects and arrays. You may think I'm silly, but I remember it by saying "Porky Pig". "Porky" sounds like "for key". Like "forky porky". And the "Pig" is the big fat collection. "Porky in Pig" is "for key in collection." My mind may work in weird ways, but maybe you'll remember this too. ;)
1

I think you are confused with JSON object manipulations. While playing around with JSON Objects, Learn a few things which generally helps.

Fetching value from JSON Object:

var obj = {"ETH":{"USD":188.01},"BTC":{"USD":10330.41}};

For fetching at the initial level:

 obj["ETH"] // return {"USD":188.01}

Fetching at nested level:

 obj["ETH"]["USD"] // return 188.01

In your case, you are printing keys only

Update your syntax:

for(let item in data) {
    console.log(data[item]) 
 }

Output:

{
  "USD": 187.98
}
{
  "USD": 10302.56
}

2 Comments

The part that's still unclear to me is why console.log(data[item]) works, but console.log(data.item) returns undefined. Both ways should work on an object, as they do for the info var in the console itself e.g. info.ETH.USD.
When you try data.item, the item should be present as a key in data which is not the case. Item is a variable which holds the key. Thus we need to use it like data[item]

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.