move

🚚 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[], options: { from: number; to: number }): T[]

Parameters

  • arr: The source array.

  • options.from: The index of the element to move.

  • options.to: The index to move the element to.

Returns

A new array with the element moved.

Examples

TypeScript
move([1, 2, 3], { from: 0, to: 2 }); //=> [2, 3, 1]

move([1, 2, 3], { from: 0, to: 5 }); //=> [2, 3, 1] (capped at array length)

move([1, 2, 3], { from: 5, to: 0 }); //=> [1, 2, 3] (invalid source index returns original)

Try it

Last updated on
Edit this page