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

91
Vistas
How to count downwards with forEach loop javascript

So I want to go through an list of objects but I cant index it but can use forEach to go through the elements... My problem is I need to go from the last Object to the first but dont know how to do that with the forEach function! If i'd do a for loop it would look something like this:

var test = [1,2,3,4,5]
   for (let i = test.length; i > -1; i=i-1) {
       console.log(test[i])
   }

But as I explained I somehow cant index it. So can you help me?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can reverse() the array before the loop:

var test = [1,2,3,4,5]
 
test.reverse().forEach(e => console.log(e));

about 4 years ago · Juan Pablo Isaza Denunciar

0

This is a variant of Severin.Hersche's approach:

const test = [1, 2, 3, 4, 5];
for (let i = test.length;i--;) console.log(test[i]);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here is the code for your problem:

var test = [1, 2, 3, 4, 5]
    for (let i = test.length - 1; i >= 0; i--) {
      console.log(test[i])
    } 

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