Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the previous and next elements of an array loop in JavaScript?

In a simple array loop of Javacript as

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 previous and next elements in an unlimited cycle. For example,

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.

like image 899
Googlebot Avatar asked Jan 17 '13 21:01

Googlebot


People also ask

How do I find the first and last elements of an array?

The first and last elements are accessed using an index and the first value is accessed using index 0 and the last element can be accessed through length property which has one more value than the highest array index. The array length property in JavaScript is used to set or return the number of elements in an array.

How do I find the previous element of an array?

The prev() function moves the internal pointer to, and outputs, the previous element in the array.

Which method in JavaScript removes and returns the last item?

The pop() method removes the last element from an array and returns that element. This method changes the length of the array.


2 Answers

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 +len when getting the previous: the reason we need this is to avoid negative indexes, due to the way modulus works (very unfortunately, -x% is -(x%))

like image 97
Denys Séguret Avatar answered Sep 24 '22 11:09

Denys Séguret


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 way to 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 only there 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; 
like image 36
Paolo Avatar answered Sep 24 '22 11:09

Paolo