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

94
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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