João
João

Reputation: 341

normal reverse for loop to reverse jquery .each

Want to perform a for loop but it has to be a reverse loop like the following:

for (var i = arguments.length; i > 0; i -= 2)

i am doing it like this without being able to achieve the desired result:

 $.each(arguments.reverse(), function(i, result)

I understand that with reverse i will loop from the end to the begining of the array.

But how can i do that two at a time and stop when i = 0?

Upvotes: 0

Views: 82

Answers (2)

PeterKA
PeterKA

Reputation: 24648

You want .each() to process indices: 0, 2, 4:

$.each($.grep(arguments.reverse(),function(v,i)  { return i%2 === 0; }), function(i, result)

Upvotes: 0

Viswanath Donthi
Viswanath Donthi

Reputation: 1821

This is one way to do it:

$.each(arguments.reverse(), function(i, result){
    if(i%2 == 0){
       // Do your task here
    }
});

Upvotes: 1

Related Questions