Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9151479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:48:45+00:00 2026-06-17T11:48:45+00:00

In a simple array loop of Javacript as for (var i=0; i<array.length; i++) {

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:48:46+00:00Added an answer on June 17, 2026 at 11:48 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple array with bank holidays: var bank_holidays = ['06/04/2012','09/04/2012','07/05/2012','04/06/2012','05/06/2012','27/08/2012','25/12/2012','26/12/2012','01/01/2013','29/03/2013','01/04/2013','06/05/2013','27/05/2013']; I want
I'm reading this post . It defines a simple array: var myArray = [1,
Just want to loop an array in javascript. This is my base array var
I need help with a loop... it's probably simple but I'm having difficulty coding
I have an simple array array = [apple, orange, lemon] array2 = [[apple, good
I have a simple array with x number of items. I am displaying them
I have a very simple array (please focus on the object with "points.bean.pointsBase" as
I have a simple array of stuff: $array = array(apples,oranges,strawberries); I am trying to
I want to display a simple array of strings in a ListPopupWindow that is
For checking equality in a simple array, i've the following; int a[4] = {9,10,11,20};

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.