Angular 如何选择加密货币的当前价格?从API获取ID为的硬币(CoingeckoAPI)

Angular 如何选择加密货币的当前价格?从API获取ID为的硬币(CoingeckoAPI),angular,typescript,Angular,Typescript,当我得到这样的回答时,我不确定如何选择硬币的当前价格: 因此,在这种情况下,我已经接受了比特币的当前价格,如下所示: getCoin() { return this.cryptoAPIService.getCoinById(this.id).subscribe(res => { console.log(res); this.currentPrice = this.numberWithCommas(res[22].current_price.usd); });

当我得到这样的回答时,我不确定如何选择硬币的当前价格:

因此,在这种情况下,我已经接受了比特币的当前价格,如下所示:

getCoin() {
   return this.cryptoAPIService.getCoinById(this.id).subscribe(res => {
     console.log(res);
     this.currentPrice = this.numberWithCommas(res[22].current_price.usd);
 });
但当我点击以太坊时,反应如下:

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

现在我得到了错误,因为我必须像这样选择以太坊的当前价格:
res[23]。当前价格.usd
。其中一些在24指数上有当前价格,因此我不确定如何使用一个函数选择所有这些的当前价格,有什么建议吗

编辑:

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

使用
switch
语句有条件地处理不同的响应类型

开关(res[0]){
“比特币”案例:
// ...
打破
“以太坊”案例:
// ...
打破
}

如果要将数据映射到数组,请不要这样做

删除此行:

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

是的,但我有一张50枚硬币的清单,是从coingecko API中获得的。如果它们都不同,你需要单独处理它们。如果有些是相同的,使用级联开关的情况。是的,我不明白为什么我得到这样的响应作为一个数组,而不是像你可以看到的图像上的对象。我在调用API的地方提供了u代码,请检查。@Kruno我已经根据你的新代码更新了我的答案。