Here's an example function that takes an array arr
, a central index idx
, and a window size size
. The function returns an object with two properties - left
and right
. left
is the index to the left of idx
, while right
is the index to the right of idx
. If idx
is at the beginning or end of arr
, the offsets will "wrap around" to the other side of the array.
index.tsx303 chars17 lines
You can call this function with an array, an index, and a size to get the offset indices:
index.tsx166 chars8 lines
In this example, the function returns { left: 3, right: 2 }
because the left offset of index 0 is index 3 (which "wraps around" to the end of the array), and the right offset is index 2. You can then use these offset indices to slice the array:
index.tsx102 chars5 lines
Here, the subset
array consists of the values at indices 5, 0, 1, and 2 from the original arr
array.
gistlibby LogSnag