Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
Gráficos de datos de productos básicos: problema al obtener las fechas y los valores en JSON
 { "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 } } } }

Arriba está el JSON que se devuelve de mi consulta axios. Intenté usar un ciclo y forEach para capturar la fecha y el valor en USD para una x, y de un gráfico, pero parece que no puedo obtener ambos al mismo tiempo. A continuación se muestra el código de mi gráfico hasta ahora.

 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]; //issue is here - I cannot figure out how to loop through rates to separate the date and the USD value to add to the chartArray. chartArray.push({x: date, y: rate}); new Chart(document.getElementById("myChart04"), { type: 'line', data: { labels: chartArray.map(function(a) { return ax; }), datasets: [{ data: chartArray, label: "RUB | USD", borderColor: "#f93154", backgroundColor: "#fee3e8", fill: true }] }, options: { response: true, scales: { y: { beginAtZero: true } } } }); }); }

Cualquier consejo sobre cómo hacer esto sería muy apreciado. Sé que me estoy perdiendo algo simple.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede recorrer las claves de rates usando Object.keys y luego empujar a 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)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!