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

  • Home
  • SEARCH
  • 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 860981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:54:15+00:00 2026-05-15T08:54:15+00:00

First, apologies,this should be simple but I’ve had too much coffee and cannot wrap

  • 0

First, apologies,this should be simple but I’ve had too much coffee and cannot wrap my tired brain around this (at least not without making it way more complicated than I know it should be).

Lets say I have a simple Javascript array with a number of items in it:

var items = ["Hello", "This", "is", "an", "array", "with", 
             "a", "number", "of", "items", "in", "it"];

For whatever reason I’m suddenly interested in the 2nd value:

items[1]
//-> "This"

But I also want to get the previous value, and the next two values…

//-> "Hello", "This", "is", "an"

To put it this way:

function getBatch(items, start) {
  // What goes here so it would return the results below?
}

getBatch(items, 0);
//-> ["it", "Hello", "This", "is"]

getBatch(items, 4);
//-> ["an", "array", "with", "a"]

getBatch(items, (items.length-1));
//-> ["in" "it", "Hello", "This"]

What is the code for the function getBatch (above) in order to return those result-sets?

Please, no answers dependent on JQuery 🙂

  • 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-05-15T08:54:16+00:00Added an answer on May 15, 2026 at 8:54 am

    Edited: (removed original version, as it was more crap than this)

    function getBatch(items, start) {
      var tmpArr = items;
      tmpArr.splice(0,0,items);
    
      var offset = (start > items.length-3) ? 0 : items.length;
    
      return tmpArr.slice(start+offset-1,start+offset+3);
    }
    

    Edit 2.1 (bugfixed)

    Edit 2.2 (move start to actual start and eliminate one wrap case (final)

    Ok, crying boy cared for by his mother. Now, let’s do this correct.

    function getBatch(items, start) {
      // Behaviour not defined by example
      if(items.length < 4)
        return items;
    
      // Set start to actual start (start-1), and
      // ensure that start is always within 0 - items.length
      start = ((start-1)+items.length) % items.length;
    
      // First take care of the easy case.
      if(start < items.length-3) return items.slice(start,start+4);
    
      // Last x elements + (4-x) elements from beginning of array
      return items.slice(start).concat(items.slice(0,4-(items.length-start)));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(apologies if this is a duplicate ... i posted but saw no evidence that
First off, I apologize if this doesn't make sense. I'm new to XHTML, CSS
First and foremost, I apologize for any vagueness in this question. At this point,
First, let's get the security considerations out of the way. I'm using simple authentication
First of all, I know how to build a Java application. But I have
First off, there's a bit of background to this issue available on my blog:
Apologies I can't post any code at this time cos I'm away from my
In order to specify some things first: The user should be able to create
(Apologies in advance if this question is short on details, I'll watch the comments
Apologies if this question has been asked before. I'm hoping that someone can step

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.