3

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Onyx
  • 5,186
  • 8
  • 39
  • 86
  • Your index values and the expected returns do not make a lot of sense to me. Wouldn't the first example be index 0, the second 5 and the third 3? – Steven Kuipers May 12 '20 at 12:17

3 Answers3

3

You can add index handling. If index is less that 0 = add length of array to index, if index is more than array length - substract array length from it. It will solve your problem

let array = ['1', '2', '3', '4', '5', '6', '7']

function getArr(arr, idx) {
let newArr = []

     for(let i= idx -2; i <= idx +2; i++) {
       let currentindex = i
       if(i < 0) {
         currentindex = i + arr.length
       } else if( i >= arr.length) {
          currentindex = i - array.length
       }
       newArr.push(arr[currentindex])
     }
return newArr
}

console.log(getArr(array, 0))
console.log(getArr(array, 2))
console.log(getArr(array, 5))
elvira.genkel
  • 1,303
  • 1
  • 4
  • 11
1

This is a declarative (functional programming) approach:

If the array is a and given index is i we can imagine desired array is:

[i-2, i-1, i, i+1, 1+2]
// Unfortunately Javascript doesn't support [start..end] syntax

But if we try to use that as indices to our array (a) some indices may be out of list unless we can access the list items in a circular way. For example in [1, 2, 3] the 5th item would be 2 (5 mod 3). Such a function should be already written so by a little search:

const arrayItem = (a, i) => a[(i % a.length + a.length) % a.length]

Now here are the steps to convert input array to output as a function named f which takes input array (a) and index (i):

const f = (a, i) => [i - 2, i - 1, i, i + 1, i + 2].map(x => arrayItem(a, x))

or writing it in without dependency:

const array = ['1', '2', '3', '4', '5', '6', '7']

const f = (a, i) => [i - 2, i - 1, i, i + 1, i + 2]
  .map(x => a[(x % a.length + a.length) % a.length])

console.log(f(array, 2))
console.log(f(array, 0))
console.log(f(array, 5))
Xaqron
  • 29,931
  • 42
  • 140
  • 205
0

One solution would be to wrap your array with duplicates. Then you can add the length of the array to the index and create your array. This nicely handles edge cases.

function getArr(arr, idx) {
  let ary = [...arr, ...arr, ...arr]
  let ptr = idx+arr.length;
  return ary.slice(ptr-2, ptr+3);
}

let array = ['1', '2', '3', '4', '5', '6', '7']
console.log(getArr(array, 0))
console.log(getArr(array, 2))
console.log(getArr(array, 5))
Joseph Cho
  • 4,033
  • 4
  • 26
  • 33