R
fn

swap

v0.0.10tested

Return a new array with the items at indices `a` and `b` swapped. Returns a shallow copy unchanged when either index is out of range or equal. Never mutates.

Example

ts
swap(['a', 'b', 'c'], 0, 2); // ['c', 'b', 'a']

Signature

ts
export function swap<T>(array: readonly T[], a: number, b: number): T[]{ ... }

Type Parameters

T

Parameters

ParameterTypeDescription
arrayreadonly T[]The source array
anumberThe source array
bnumberFirst index

Returns

T[]A new array with the two items swapped