Search code examples
angulartypescript

How can I select current price of cryptocurrency? Get an coin with ID from API (CoinGecko API)


I am not sure how am I able to select current price of coin when I get response like this: enter image description here

So in this case I have accesed to a current price of bitcoin like this:

getCoin() {
   return this.cryptoAPIService.getCoinById(this.id).subscribe(res => {
     console.log(res);
     this.currentPrice = this.numberWithCommas(res[22].current_price.usd);
 });

but when I click on ETHEREUM, response look like this:

enter image description here

now I get the error because I have to select current price of ethereum like this: res[23].current_price.usd. Some of them have current price on index of 24, so I am not sure how am I able to select the current price of all of them with one function, any suggestions?

EDIT:

getCoinById(id: any) {
    return this.http.get("https://api.coingecko.com/api/v3/coins/"+id).
    pipe(
    map(data => Object.keys(data).map(k => data[k]))
    );
  }

Solution

  • You are mapping the data to an array, do not do that.

    Remove this line:

      map(data => Object.keys(data).map(k => data[k]))