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

Discussão em 'Angular' iniciado por Kruno, Fevereiro 23, 2025.

  1. Kruno

    Kruno Guest

    I am not sure how am I able to select current price of coin when I get response like this: [​IMG]

    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:

    [​IMG]

    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]))
    );
    }

    Continue reading...

Compartilhe esta Página