9

I'm parsing an array using $.each(), but inside it, I'm using .splice() method, so I need to iterate backward. Is it possible ?

var store = [...];
//...
var rules = [...];
//...
$.each(store, function(index, element) { // This loop needs to iterate backward
    $.each(rules, function(index2, rule) {
        if (element.id == rule.id) {
            store.splice(index, 1);
        }
    });
});

WARN :

  • I don't want to reverse the array, it wouldn't be the same behavior.
  • Also I know I could use for, I just want to know if it's achievable using $.each
Elfayer
  • 4,411
  • 9
  • 46
  • 76
  • Possible duplicate of http://stackoverflow.com/questions/1394020/jquery-each-backwards – Filippos Karapetis Jul 24 '14 at 10:09
  • 4
    Nop, I've seen this post, it's using a `.reverse()` on the array. Please, fully read the question. – Elfayer Jul 24 '14 at 10:09
  • I fail to see why can't you just use hash here (then filter based on this hash). – raina77ow Jul 24 '14 at 10:13
  • 1
    See this post : http://stackoverflow.com/questions/9882284/looping-through-array-and-removing-items-without-breaking-for-loop To use correctly `.splice`, you have to iterate backward. And `$.each()` isn't basically. – Elfayer Jul 24 '14 at 10:14
  • 1
    Why can you use a simple `for` loop? – Satpal Jul 24 '14 at 10:47
  • 1
    I could as I wrote in the question. And I finally did.^^ But I wanted to know if it was doable using `$.each()`. – Elfayer Jul 24 '14 at 13:12

3 Answers3

3
$.each(store.reverse(), function(index, element) { // This loop iterate backward
[...]

This works, it's the same as this post jquery-each-backwards. But the point here is, you're applying this over the store.

Masoud Keshavarz
  • 2,166
  • 9
  • 36
  • 48
Kadaiser
  • 219
  • 3
  • 10
1

You could use $.each but you'd have to decrement the indice variables (index, index2) as you did.

Michael Angstadt
  • 880
  • 10
  • 18
0

$.each() itself cannot be iterated backwards as there is no modifier for it. The input it takes is from a selector. which can be converted into an array. Then $.each() iterates forwards by index (0,1,2,... and so on).

The only way to reverse flow is to either Array.reverse() or find an iterator loop that does so. You can also build a recursion function that serves your purpose. But all these options are outside the limitation of $.each()

Here are options from iteration loops to consider:

-> for loop (reverse)

var value = 10;
for (var i = value; i; i--){
    console.log(i);
    //bonus
    //return back to a proper flow is to add the whole value it
    console.log(value + i);
}

-> while loop (reverse):

var X=9,Y=X;
while (--Y) {
    console.log(Y);
    //will only stop at 1 since while cannot return a falsy value
    //0 is considered a falsy value, and is nullified from the loop
}

Options to consider to reverse array iteration:

-> Array.reverse().forEach()

As my late mentor once said, There is more than one way to carve an elephant from stone. This site exists because many people have creative ways to solve issues. Unfortunately, there isn't anything built into $.each() that will run backwards stand-alone, hence people resorting to other means (some of which are mentioned above). Im sorry this isnt a satisfactory "yes" answer to the question, but hopefully explains why $.each() can only loop forward. If this is me making my own solution it would be this:

var loopy=(value,output)=>{
    for(var i = value; i; i--){
        output.call(this,{
           index:{
               forward:()=>value-i,
               reverse:()=>i-1
           },
           count:{
               forward:()=>1+value-1,
               reverse:()=>i
           }
        })
    }
}

loopy(10, i=>console.log( i.index.forward() ))

The function above allows you to have options ranging whole counts and index counts. This is similar to how Array.forEach() works except youre outputting an object and its values per iteration. I use a function similar to this to fetch object's values, keys, and indexes.

Gareth Compton
  • 159
  • 1
  • 12