📥 Inserts items at the specified index into an array.
Info
Immutable: This function does not mutate the original array.
Syntax
TypeScript
import { insert } from '@opentf/std'; insert<T>( arr: T[] = [], indexOrFn: number | null | undefined | ((item: T, index: number, array: T[]) => boolean), ...items: T[] ): T[]
Parameters
arr: The source array.indexOrFn: The index where the new items should be inserted, or a predicate function to find the insertion point. Ifnullorundefined, items are appended to the end....items: The items to be inserted. When using a predicate, the last argument can be'before'or'after'to control insertion position (default:'before').
Returns
A new array with the inserted items.
Examples
TypeScript
insert([]) //=> [] insert([1]) //=> [1] insert([1], 0) //=> [1] insert([1], 0, 0) //=> [0, 1] insert([1], 1, 0) //=> [1, 0] insert([1, 2, 3], 1, 5) //=> [1, 5, 2, 3] insert([1, 2, 3], -1, 5) //=> [1, 2, 5, 3] insert([1, 2, 3], -3, 5) //=> [5, 1, 2, 3] insert([1, 2, 3], 3, 5) //=> [1, 2, 3, 5] insert([1, 2, 3], 3, 5, 6) //=> [1, 2, 3, 5, 6] insert([1, 2, 3], null, 5, 6) //=> [1, 2, 3, 5, 6] insert([1, 2, 3], 0, [5, 6]) //=> [[5, 6], 1, 2, 3] insert([1, 2, 3], 0, ...[5, 6]) //=> [5, 6, 1, 2, 3] // Predicate-based insert insert([1, 2, 3], (x) => x === 2, 5) //=> [1, 5, 2, 3] insert([1, 2, 3], (x) => x === 2, 5, 'after') //=> [1, 2, 5, 3]