Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

90
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!