Array
swap

Swaps two elements in an array.

Immutable: This does not mutate the given array.

Syntax

import { swap } from '@opentf/std';
 
swap<T>(arr: T[], x: number, y: number): T[]

Examples

swap([], 0, 0) //=> []
 
swap([], 0, 1) //=> []
 
swap([0], 0, 1) //=> [undefined, 0]
 
swap([1, 2, 3], 1, 2) //=> [1, 3, 2]
 
swap([1, 2, 3], 1, 5) //=> [1, undefined, 3, , , 2]
 
const arr = [{ a: 1 }, { b: 'a' }, { c: [5] }];
swap(arr, 0, 2) 
//=> [
//  {
//    c: [5],
//  },
//  {
//    b: 'a',
//  },
//  {
//    a: 1,
//  },
// ]

Try