Chartjs-Charting Commodity Data โ€“ Issue getting the dates and values in JSON

1๐Ÿ‘

โœ…

You can loop through the keys of rates using Object.keys and then push to the chartArray

let results = {
  "data": {
    "success": true,
    "timeseries": true,
    "start_date": "2022-02-01",
    "end_date": "2022-03-02",
    "base": "RUB",
    "rates": {
      "2022-02-01": {
        "USD": 0.012922641020611143
      },
      "2022-02-02": {
        "USD": 0.013024953827785636
      },
      "2022-02-03": {
        "USD": 0.013111232627449121
      },
      "2022-02-04": {
        "USD": 0.013080360583812869
      }
    }
  }
}

let chartArray = [];
Object.keys(results.data.rates).forEach((key) => chartArray.push({x: key,y: results.data.rates[key]["USD"]}))

console.log(chartArray)

Leave a comment