我想使用 CoinGecko API 显示一些数据。我在显示价格时遇到了一些问题,因为令牌 ID 的名称中包含“-”。
当我尝试使用 显示对象document.getElementById("price").innerHTML = data.luffy-inu.usd;
时,控制台给我以下错误:
ReferenceError: inu 未定义
这就是我获取数据的方式:
fetch("https://api.coingecko.com/api/v3/simple/price?ids=luffy-inu&vs_currencies=usd")
.then((response) => {
if (response.ok) {
return response.json();
} else {
throw new Error("NETWORK RESPONSE ERROR");
}
})
.then(data => {
console.log(data);
document.getElementById("price").innerHTML = data.luffy-inu.usd;
})
.catch((error) => console.error("FETCH ERROR:", error));
我想我必须在获取数据后去掉“-”。任何线索如何解决?我尝试使用 map() 更改名称,但没有成功。