Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

130
Visualizações
Unable to get object with date format from JSON file in JavaScript

I'm doing an activity with JavaScript where I must call an extern API and get the information that it contains, the JSON file structure is the next:

"dates": {
    "2020-03-22": {
        "countries": {

The problems is that when I get the information I put it in a variable named 'info' and to arrive to the date camp I have put the date text in another variable to concatenate with the previous object, but the problem is that returns 'undefined' value.

Here's the code I made to get the information:

function callAPI() {
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function() {
        if(this.readyState == 4 && this.status == 200){
            var info = JSON.parse(this.responseText);
            var dateApi = "2020-03-22";

            console.log(info.dates.dateApi);
        }
    }
    xmlhttp.open("GET", "https://api.covid19tracking.narrativa.com/api/2020-03-22/country/spain");
    xmlhttp.send();
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Change info.dates.dateApi code to info.dates[dateApi] this should work

function callAPI() {
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function() {
        if(this.readyState == 4 && this.status == 200){
            var info = JSON.parse(this.responseText);
            var dateApi = "2020-03-22";

            console.log(info.dates[dateApi]);
        }
    }
    xmlhttp.open("GET", "https://api.covid19tracking.narrativa.com/api/2020-03-22/country/spain");
    xmlhttp.send();
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda