Move Array Element
Repositions an element in an array from one index to another.
moveArrayElement
The moveArrayElement function repositions an element in an array from one index to another, returning a new array with the updated order.
Function Signature
function moveArrayElement<T>(arr: T[], fromIndex: number, toIndex: number): T[]Parameters
arr(T[]): The original array to operate on.fromIndex(number): The index of the element to move.toIndex(number): The target index where the element should be placed.
Returns
T[]: A new array with the element moved to the desired position.
Example
playground.ts
Notes
- Returns a new array with the element moved — does not mutate the original array.
- If
fromIndexandtoIndexare equal or out of bounds, behavior mimics JavaScript’ssplicemechanics without throwing errors.
Last updated: Sun, Jun 14, 2026 07:06:16AM (UTC)
