jQuery $.each() reverse/backward iteration
Asked Answered
S

3

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
Somnolent answered 24/7, 2014 at 10:7 Comment(6)
Possible duplicate of https://mcmap.net/q/79898/-jquery-each-backwardsColonist
Nop, I've seen this post, it's using a .reverse() on the array. Please, fully read the question.Somnolent
I fail to see why can't you just use hash here (then filter based on this hash).Underslung
See this post : #9882784 To use correctly .splice, you have to iterate backward. And $.each() isn't basically.Somnolent
Why can you use a simple for loop?Scarrow
I could as I wrote in the question. And I finally did.^^ But I wanted to know if it was doable using $.each().Somnolent
C
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.

Colin answered 25/6, 2020 at 11:31 Comment(1)
This method of reversing works perfectly.Freestanding
M
1

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

Magulac answered 2/7, 2015 at 4:26 Comment(0)
L
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.

Lory answered 3/2, 2021 at 18:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.