Get the 5 items before and after the current index in an array

Say I have an array that looks like this

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

These array represents page numbers in my scenario. Say if I am on page 8, I'd like to create a seperate array that includes the following The first 5 before page 8 and the first 5 after page 8.

i.e first 11 items array.

[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

Therefore having 11 items in an array including that page itself.

If the array is smaller than 5, then simply return the rest.

i.e

if the array looks like this [2,3,4,5,6,7,8] and if the page is 4, since the before does not have 5 items exactly I'd like to get all of it.



from Recent Questions - Stack Overflow https://ift.tt/3mq86TS
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation