Skip to content

Home

Offset array elements

Moves the specified amount of elements to the end of the array.

const offset = (arr, offset) => [...arr.slice(offset), ...arr.slice(0, offset)];

offset([1, 2, 3, 4, 5], 2); // [3, 4, 5, 1, 2]
offset([1, 2, 3, 4, 5], -2); // [4, 5, 1, 2, 3]

More like this

Start typing a keyphrase to see matching snippets.