Array.prototype.slice()
twice to get the elements after the specified index and the elements before that....
) to combine the two into one array.offset
is negative, the elements will be moved from end to start.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]
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️