{
"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
}
}
}
}
This JSON data is the response from an axios query. I have been attempting to extract both the date and USD value for chart plotting using a loop and forEach method, but I am encountering difficulties in obtaining them simultaneously. Check out my current progress with the chart code below.
function fnChart04() {
var chartArray = [];
var yesterday = moment().subtract(24, 'hours').format('YYYY-MM-DD');
var url = "https://commodities-api.com/api/timeseries?access_key=XYZed&start_date=2022-02-01&end_date="+yesterday+"&base=RUB&symbols=USD";
var header = { headers: {'Accept': 'application/json; odata=nometadata'} };
axios.get(url, header).then(function (response) {
var results = response.data;
let rate = results.data.rates.USD;
let date = results.data.rates[0];
// Struggling here - unable to effectively loop through rates to separate date and USD value for insertion into chartArray.
chartArray.push({x: date, y: rate});
new Chart(document.getElementById("myChart04"), {
type: 'line',
data: {
labels: chartArray.map(function(a) { return a.x; }),
datasets: [{
data: chartArray,
label: "RUB | USD",
borderColor: "#f93154",
backgroundColor: "#fee3e8",
fill: true
}]
},
options: {
response: true,
scales: {
y: {
beginAtZero: true
}
}
}
});
});
}
If you have any pointers or advice on how I can achieve this task more efficiently, I would greatly appreciate it. It seems like there's a simple solution that I'm overlooking.