0

I'm a beginner and I am working with an API. After searching the forums I know doing something like what I am asking probably isn't good practice. But the API(Cryptocompare) I am working with seems to have only one endpoint that returns the full name of a coin. And, that endpoint lists all available coins, and doesn't have a query parameter(as far as I can tell in the docs) to get the info for just one coin.

So the options I am left with is traversing the Json response using a recursive function to find if a value exists in a nested object, and then returning that object, then writing the object path to get the full name. Or to traverse the Json response, with a pre-written path and inserting user input into that path. I'm sorry if this block of text is making no sense. I hope this example makes it more clear.

Object{
    Data:{
        BTC:{
            FullName:Bitcoin
        }
        ETH: Object
    }
}

So I want to get the name Bitcoin. Normally, I could just use another endpoint, which would have a query parameter specifying which coin to look for and access the full name with something like

responseJson.Data[i].CoinInfo.FullName

But like I said, as far as I can tell, the only the endpoint which returns the full name is the one that lists all coins. So I want to be able to do something like this

let searchedCoin = "BTC";
responseJson.Data.searchedCoin.FullName

Does something like that exist or is this just a big no-no?

Kaleidics
  • 177
  • 3
  • 16
  • 4
    `responseJson.Data[searchedCoin].FullName`. Because `foo.bar` is equivalent to `foo["bar"]` in JavaScript. – Amadan Dec 26 '18 at 06:08
  • I can't believe the answer was so simple. I feel so defeated, thank you though. – Kaleidics Dec 26 '18 at 06:15
  • Most things you know are simple, once you know them. There's no shame in not knowing, only in refusing to learn. Cheers :) – Amadan Dec 26 '18 at 06:16

0 Answers0