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

252
Visualizações
.push() is not working outside of the for loop

I have already handled a response from an API call. I then export the data into a function that i'm using a for loop inside. The const diomerda is passed to the ejs file where i am outputting the array data from the API. However the data in the array is over 5000 entries, so i want to run a for loop to populate the ejs file with the entire array. But when I use loop.push it does not populate the const loop. console.log(loop) just reads [].

Once the const loop = [] is populated with the array i was then going to pass the data into the const diomerda = [json.entries[loop].character.name]

I'f anyone has any other ideas on how to acheive what i'm trying to do then please feel free to send away. Thanks in advance.

    .then(response => response.json())
    .then(json => ejsoutput(json))
}

function ejsoutput(json) {
    const loop = []
    console.log(loop)
    const diomerda = [json.entries[0].character.name, json.entries[1].character.name, json.entries[2].character.name]
    for (var i = 0; i < json.entries.length; i++) {
        loop.push(i)
    }
    res.render('index', {
        leaderboard: diomerda
    })
}
});
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Once the const loop = [] is populated with the array i was then going to pass the data into the const diomerda = [json.entries[loop].character.name]

If what you want is to populate diomerda with each character name return from the api, do this

function ejsoutput(json) {
    const diomerda = [];
    for (var i = 0; i < json.entries.length; i++) {
        diomerda.push(json.entries[i].character.name);
    }
    console.log(diomerda);
}

OR in simpler terms

function ejsoutput(json) {
    const diomerda = json.entries.map(item => item.character.name);
  
    console.log(diomerda);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

To debug, print (log) the json object, instead of the empty loop array (shoule get you on track).

Json should be a promise. That's probably what you're running into. See: https://developer.mozilla.org/en-US/docs/Web/API/Response/json

Try something like

json().then(function(json) {
      
});
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