I'm trying to figure out how to extract 5 array elements from an array based on a single index. The index is gonna be of the element in the middle of those 5 array elements. For example, if I have this array:
let array = ['1', '2', '3', '4', '5', '6', '7']
and an index of 2, I would like to get this array returned to me:
['1', '2', '3', '4', '5']
if the index is 0, I would like to get this array returned to me:
['6', '7', '1', '2', '3']
if the index is 5, I would like to get this array returned to me:
['4', '5', '6', '7', '1']
I hope I'm being able to convey what I'm trying to do. I want the element that corresponds to the index to be in the middle of the array and have the 2 previous and 2 next elements appended as well.
As you can see, when the index is at the end of the array and there are no more elements to append after the index, I begin appending the elements from the start. Vice-versa, when the index is at the start of the array, I prepend elements from the ending of the array. This is the part I can't figure out.