Array
move

🚚 Moves an array element from one index to another.

Immutable: This function does not mutate the original array.

Syntax

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

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)

Related

Try