Javascript 获取数组中的每个第 n 项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4806968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:11:23  来源:igfitidea点击:

Get every nth item in array

javascriptjquery

提问by hannu

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

我有一组 HTML 元素。我正在检查数组中的前一个对象是否存在,如下所示:

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) == 0might be the answer, but with my limited knowledge of Javascript, I don't know how to apply it to what I have now.

我相信使用jQuery.grep()和检查是否(i % 4) == 0可能是答案,但由于我对 Javascript 的了解有限,我不知道如何将它应用于我现在拥有的。

Anyone can help? Thanks!

任何人都可以帮忙吗?谢谢!

采纳答案by user113716

You can use the modulus operatorin 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;
    }
});

回答by glenatron

Can you not just use a for loop?

你不能只使用 for 循环吗?

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.

我对 JQuery 不是特别感兴趣,但在常规 JavaScript 中可以正常工作。

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 previousTopis 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.

previousTop在你做任何你需要做的事情之前,这里范围更广的是每四个项目设置一次。然后,当您到达下一个第四个元素时,您的临时变量中将获得前一个元素的值,这听起来与您对下面@patrick dw 的回答的响应相似。

回答by developer

Use an iteration with 4 as the incrementing step and not the usual 1.

使用 4 作为增量步而不是通常的 1 的迭代。