How to iterate over a list of elements from last element to first using underscore

underscore.js

how do i iterate over a list from end to beginning using _.each in underscore?

lister = ['a', 'c', 'd', 'w', 'e'];
_.each(_.range(lister.length, 0 ,-1), function (val,i) {
   console.log(val);
}, lister);

this prints number 5 to 1 in console. Is it a good idea to use underscore's _.each in place of tradition "for" loop?

Best Solution

Just reverse the array before you iterate over it?

lister.reverse();

To answer your _.each() vs for loop question, have a look here.

Related Question