Skip to content

insertAt

Inserts an item into an array at a given index.

Usage

ts
import { insertAt } from 'tsu'

insertAt([1, 2, 4, 5], 2, 3)
// [1, 2, 3, 4, 5]

insertAt([2, 3], 0, 1)
// [1, 2, 3]

insertAt([1, 2], 10, 3)
// [1, 2, 3] - an index beyond the end appends

insertAt([1, 2, 4], -1, 3)
// [1, 2, 3, 4] - a negative index counts back from the end

Type Definitions

ts
/**
 * @param array - The array.
 * @param i - The index to insert the item at.
 * @param item - The item to insert.
 * @returns A copy of the array with the item inserted.
 */
function insertAt<T>(array: readonly T[], i: number, item: T): T[]

Released under the MIT License.