在 Javacript 的简单数组循环中
for (var i=0; i<array.length; i++) {
var previous=array[i-1];
var current=array[i];
var next=array[i+1];
}
我需要在无限循环中获取previous
and元素。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
什么是最有效的方法来做到这一点。我能想到的唯一方法是在每一轮中检查元素是数组中的第一个还是最后一个。
事实上,我希望以某种方式使数组成为一个封闭的循环,而不是线性的。