-1

Using nomonics crypto api and Javascript with a googlesheets spreadsheet, the returned object is:

[
  {
    "circulating_supply": "17591362",
    "high": "24436.29525000",
    "high_timestamp": "2018-01-05T00:00:00.000Z",
    "market_cap": "70647474650.63",
    "max_supply": "21000000",
    "price": "4016.03211000",
    "currency": "BTC",
    "1d": {
      "market_cap_change": "2334344443.63",
      "market_cap_change_pct": "0.0342",
      "price_change": "132.26230000",
      "price_change_pct": "0.0341",
      "volume": "531837882.13",
      "volume_change": "34633070.11",
      "volume_change_pct": "0.0697"
    },
    "7d": {
      "market_cap_change": "2087829209.46",
      "market_cap_change_pct": "0.0305",
      "price_change": "115.88873000",
      "price_change_pct": "0.0297",
      "volume": "2984154293.31",
      "volume_change": "-872155432.42",
      "volume_change_pct": "-0.2262"
    },
        "rank": "1"
  }
]

I can access the price with

resp[0].price

but when I try to access the 1d and 7d parts I have tried:

resp[0].1d.market_cap_change

and

resp[0]1d.market_cap_change

googlesheets does not allow me to save the code with the error:

Missing name after . operator. (line 29, file "Code")

What is the correct way to access the elements in the 1d and 7d block?

Thank you.

user1903663
  • 1,713
  • 2
  • 22
  • 44

1 Answers1

0

You need to use [] notation as your key is not valid JS identifier

resp[0]['1d'].market_cap_change

let resp = [{"circulating_supply": "17591362","high": "24436.29525000","high_timestamp": "2018-01-05T00:00:00.000Z","market_cap": "70647474650.63","max_supply": "21000000","price": "4016.03211000","currency": "BTC","1d": {"market_cap_change": "2334344443.63","market_cap_change_pct": "0.0342","price_change":"132.26230000","price_change_pct": "0.0341","volume": "531837882.13","volume_change": "34633070.11","volume_change_pct": "0.0697"},"7d": {"market_cap_change": "2087829209.46","market_cap_change_pct": "0.0305","price_change": "115.88873000","price_change_pct": "0.0297","volume": "2984154293.31","volume_change": "-872155432.42","volume_change_pct": "-0.2262"},"rank": "1"}]

console.log(resp[0]['1d'].market_cap_change)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60