Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

250
Vistas
.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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda