1

I am trying to find a way to get a range of 5 sequential indexes from an array of indeterminate length.

My array might look like this: [0,1,2,3,4,5,6,7]

I know I can use array.slice() if I need to return something like [2,3,4,5,6], but sometimes I might need to return something like [6,7,0,1,2]

Is there a clean or built-in way to do this?

1
  • There's nothing built in that automatically wraps around, you need to write that yourself. Commented Jan 5, 2022 at 20:11

1 Answer 1

-1

If I understood right, you need something like this:

function array_indexes(arr, keys){
    let res = [];
    for(let k of keys){
        res.push(arr[k]);
    }
    return res;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.