Home > other >  fetch data from api but the id comes with Hyphen -
fetch data from api but the id comes with Hyphen -

Time:10-05

so what i'm trying to do is get live prices between crypto coins it was going smoothly until some id came with dash and the problem appear

enter image description here

here is the code

var btcbnb, ethbnb, usdtbnb, usdcbnb, ltcbnb;
  var btcln, ethcln, usdtcln, usdccln, ltcln, bnbln ,bch;
  var liveprice11 = {
    "async": true,
    "scroosDomain": true,
    "url": "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum,usd,litecoin,binancecoin,binance-peg-bitcoin-cash&vs_currencies=bnb,link,usd",
  
    "method": "GET",
    "headers": {}
  }
  
  $.ajax(liveprice11).done(function (response){
    btcbnb = response.bitcoin.bnb;
    ethbnb = response.ethereum.bnb;
    usdtbnb = response.ethereum.bnb;
    usdcbnb = response.usd.bnb;
    ltcbnb = response.usd.bnb;
    // link
    btcln = response.bitcoin.link;
    ethcln = response.ethereum.link;
    usdtcln = response.usd.link;
    usdccln = response.usd.link;
    ltcln = response.litecoin.link;
    bnbln = response.binancecoin.link;
    bch = response.binance-peg-bitcoin-cash.usd;
    console.log(bch);
  });

this is the line that has the problem

 bch = response.binance-peg-bitcoin-cash.usd;

really hope someone can help ,thanks

CodePudding user response:

As you found, JS doesn't let you access object members by name that have a dash like that.

A simple workaround would be

bch = response['binance-peg-bitcoin-cash'].usd;
  • Related