🚚 Moves an array element from one index to another.

Info

Immutable: This function does not mutate the original array.

Syntax

TypeScript
import { move } from '@opentf/std';

move<T>(arr: T[], from: number, to: number): T[]

Parameters

  • arr: The source array.

  • from: The index of the element to move.

  • to: The index to move the element to.

Returns

A new array with the element moved.

Examples

TypeScript
move([1, 2, 3], 0, 2); //=> [2, 3, 1]

move([1, 2, 3], 0, 5); //=> [2, 3, 1] (capped at array length)

move([1, 2, 3], 5, 0); //=> [1, 2, 3] (invalid source index returns original)
Last updated on
Edit this page