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 7919567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:54:39+00:00 2026-06-03T15:54:39+00:00

I was wondering how to do the follow. I have an array of objects

  • 0

I was wondering how to do the follow.

I have an array of objects and I’m trying to retrieve a set amount eg of them around a given index.

Here’s an example.

var arr = [0,1,2,3,4,5,6,7,8,9,10];
var index = 0;
var max = 5;

If the index is 0 I want [0 - 4]
If the index is 4 I want [2 - 6]
If the index is 9 I want [6 - 10]

But I also want it to work if the array is smaller then the max too. Eg

var arr = [0,1,2];
var index = 0;
var max = 5;

Any index would return [0 - 2]

Any thoughts?

  • 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-03T15:54:40+00:00Added an answer on June 3, 2026 at 3:54 pm

    You might want to try this

    var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    
    function getRange(array, index, range) {
        var least = index - Math.floor(range/2);
        least = (least < 0) ? 0 : least;
        return array.slice(least, least+range);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering what type types have in c. For example, how would I do
I have been trying to follow the guidelines in this Microsoft article to authenticate
I was wondering if this code snippet is considered legal: $arr = array(123,456,789,123,456,789); foreach($arr
I have the follow HTML <div>This is some <span>special <a href=#>text</a></span> and it's super</div>
I want to create a similar feature as Twitters follow feature but wondering a
I have a function that takes either an array of IDs or a singular
I was wondering if someone could help me. Im trying to integrate some code
I have the follow function with multiple inputs let blackscholes strike assetPrice standarddev riskfreerate
I have an object (an associate array so to say - also known as
I have a Socket Client and I was wondering what would be the correct

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.