Skip to content

rotate

The rotate function shifts the elements of an array to the left or right according to the given offset.

Syntax

typescript
rotate<T>(array: T[], offset: number): T[];

Parameters

NameTypeDescription
arrayT[]Source array
offsetnumberNumber of positions to rotate

Returns

TypeDescription
T[]New rotated array

Examples

typescript
rotate([1, 2, 3, 4], 1); // => [2, 3, 4, 1]
rotate([1, 2, 3, 4], -1); // => [4, 1, 2, 3]
rotate([1, 2, 3, 4], 2); // => [3, 4, 1, 2]

Notes

  • The offset can be positive (right) or negative (left).
  • Does not modify the original array.

References

Released under the MIT License.