Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get every nth item in array

I have an array of HTML elements. I'm checking whether a previous object in the array exists like this:

var boxes = $('.box'); // creating the array

boxes.each(function(i){ // going through the array
    var prevBox = i>0?$(boxes[i-1]):false; // check whether a previous box exists
    if (prevBox) { } // do something
    else { } // do something else
});

This works well. But I would also need to check the existence of every fourth object (box) in the array, or more precisely whether an object three objects before the current one exists.

This doesn't work:

var prevBox = i>0?$(boxes[i-4]):false;

I believe using jQuery.grep() and checking if (i % 4) == 0 might be the answer, but with my limited knowledge of Javascript, I don't know how to apply it to what I have now.

Anyone can help? Thanks!

like image 848
hannu Avatar asked Jan 26 '11 16:01

hannu


People also ask

How to get the nth item in an array using JSON?

YOu can access a specific numbered instance in an array using JSON notation. For example the following would return the 9th element in the zero based array. So as long as you know what N is you can access the Nth item by typing JSON into the expressions tab where the index number is N-1

How to pick every nth value from range in an array?

We use Index function to return value from the intersection between the row number and the column number in an Array. This function will pick the number and give use as output. Row function is used to return the row number of a cell reference. So in this situation row function will help to pick every nth value from range.

How to get every element in an array in JavaScript?

Write a JavaScript program to get every n th element in a given array. Use Array.prototype.filter () to create a new array that contains every nth element of a given array.

How to retrieve nth value in row column and range?

Let’s take an example to understand how we can retrieve the Nth value in a row, column and range. We have Agent data in Excel. Column A contains agent name and column B contains revenue. From column B, we want to retrieve every nth value. Enter the formula in cell C2.


2 Answers

Can you not just use a for loop?

for ( var i=0; i< boxes.length; i+=4 )
  {
    // do stuff with boxes[i]
  }

I'm not big on JQuery specifically, but in regular JavaScript that would work fine.

EDIT: You've redescribed the problem somewhat, so you want to act on every item, but do something specific on the fourth...

var previousTop;
for ( var i=0; i< boxes.length; i++ )
  {
    // do stuff with boxes[i]
    if ( ( (i+1) % 4 ) === 0 )
      {
          previousTop = boxes[i].position.top;
          // do stuff with the fourth item
      }
  }

Here the more broadly scoped previousTop is then being set every fourth item before you do whatever else you need to do. Then when you reach the next fourth element you have the value from the previous one in your temporary variable, which sounds similar to what you are looking for from your response to @patrick dw's answer below.

like image 72
glenatron Avatar answered Oct 04 '22 02:10

glenatron


You can use the modulus operator in the loop to see if you are on a fourth interval.

Question was clarified.

var boxes = $('.box'); // creating the array

boxes.each(function(i){
    if( i >= 3 ) {
        var prevBox = boxes.eq( i - 3 );
        var pos = prevBox.position();
        var top = pos.top;
    }
});
like image 43
user113716 Avatar answered Oct 04 '22 00:10

user113716