如何在 JavaScript 中获取数组循环的上一个和下一个元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14388291/
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
How to get the previous and next elements of an array loop in JavaScript?
提问by Googlebot
In a simple array loop of Javacript as
在 Javacript 的简单数组循环中
for (var i=0; i<array.length; i++) {
var previous=array[i-1];
var current=array[i];
var next=array[i+1];
}
I need to get the previousand nextelements in an unlimited cycle. For example,
我需要无限循环获取previous和next元素。例如,
The previous element of the first element in the array is the array last element
The next element of the last element in the array is the array first element
What can be the most efficient way to do this. The only way I can think of is to check if the element is the first or last in the array in every round.
什么是最有效的方法来做到这一点。我能想到的唯一方法是在每一轮中检查元素是数组中的第一个还是最后一个。
In fact, I hope to make the array a closed cycle somehow, rather than linear.
事实上,我希望以某种方式使数组成为一个封闭的循环,而不是线性的。
采纳答案by Paolo
as you're talking about "unlimited cycle" I assume your loop is something like that
当你在谈论“无限循环”时,我认为你的循环是这样的
var i = 0,
l = array.length;
while( true ) // keep looping
{
if(i >= l) i = 0;
// the loop block
if(/* something to cause the loop to end */) break; // <-- this let execution exit the loop immediately
i+=1;
}
The most efficient wayto achieve your goal is the naive one: checking
实现目标的最有效方法是天真的方法:检查
var previous=array[i==0?array.length-1:i-1];
var current=array[i];
var next=array[i==array.length-1?0:i+1];
obviously cache the length of the array in a variable
显然将数组的长度缓存在一个变量中
var l = array.length;
and (better style) the "vars" out of the cycle
和(更好的风格)循环之外的“变量”
var previuos,
current,
next;
Note that if you are accessing the array read onlythere would be a faster (but somewhat strange) way:
请注意,如果您正在访问只读数组,则会有一种更快(但有些奇怪)的方式:
l = array.length;
array[-1] = array[l-1]; // this is legal
array[l] = array[0];
for(i = 0; i < l; i++)
{
previous = array[i-1];
current = array[i];
next = array[i+1];
}
// restore the array
array.pop();
array[-1] = null;
回答by Denys Séguret
Use modulus:
使用模数:
var len = array.length;
var current = array[i];
var previous = array[(i+len-1)%len];
var next = array[(i+1)%len];
Note the +lenwhen getting the previous: the reason we need this is to avoid negative indexes, due to the way modulus works (very unfortunately, -x%is -(x%))
请注意+len获取前一个时:我们需要它的原因是为了避免负索引,由于模数的工作方式(非常不幸的-x%是,是-(x%))
回答by sidonaldson
To add to @Denys answer - this is how you can create a reuseable function
添加到@Denys 答案 - 这就是您可以创建可重用功能的方法
var theArray = [0, 1, 2, 3, 4, 5];
var currentIndex = 0;
function getAtIndex(i) {
if (i === 0) {
return theArray[currentIndex];
} else if (i < 0) {
return theArray[(currentIndex + theArray.length + i) % theArray.length];
} else if (i > 0) {
return theArray[(currentIndex + i) % theArray.length];
}
}
// usage
getAtIndex(-2)
// you can even go crazy and it still works
getAtIndex(500)
回答by Danish
you need reduce; a built sweet function to get previous and next values of array
你需要减少;一个内置的函数来获取数组的上一个和下一个值
[0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, currentIndex, array) {
return previousValue + currentValue;
});

