Double $.each statement

Joined
Jul 9, 2023
Messages
25
Reaction score
0
Code:
$.each(idx, item)
{
    if (idx < 2)
    {
    
    }
    else
    {
            $.each(idx2, item2)
            {
    
            }
    }
}

Is the above code correct syntax?
It's a double $.each statement.
There is an if statement inside the doubled $.each statement.
I want to do if (idx < 2){} inside $.each(idx, item){}
Is the above code correct syntax?
 
Joined
Sep 4, 2022
Messages
129
Reaction score
16
you are close to need a 'recursive function', but I am not sure because of missing details.


JavaScript:
function recursive_idx(idx , item){

    if (idx < 2)
    {
     //
    }
    else
    {
      // call of the function by itself
      recursive_idx(idx , item);
    }
}
 
Last edited:
Joined
Jul 4, 2023
Messages
400
Reaction score
44
Almost correct, because according to jquery documentation $.each has notation like this:

JavaScript:
$.each(array, function( index, value ) { ... }

$.each(object, function( index, value ) { ... }

// Arrow function
$.each(array, ( index, value ) => { ... }

The array or object to iterate over.

e.g
JavaScript:
const arr1 = [ 0, 1, 2, 3, 4 ];
const arr2 = [ 5, 6, 7 ];

$.each(arr1, function(idx, item) {
  if (idx < 2) {
    console.log('arr1', idx, item);
  } else {
    $.each(arr2, function(idx2, item2) {
      console.log('arr1', idx, item,
                  'arr2', idx2, item2);
    });
  }
});
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,829
Messages
2,569,737
Members
45,525
Latest member
RosalindSa

Latest Threads

Top