insert

📥 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),
  options?: { items: T[]; position?: 'before' | 'after' }
): 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. If null or undefined, items are appended to the end.

  • options: The items to insert and, for a predicate match, an optional position ('before' by default or 'after').

Returns

A new array with the inserted items.

Examples

TypeScript
insert([]) //=> []

insert([1]) //=> [1]

insert([1], 0) //=> [1]

insert([1], 0, { items: [0] }) //=> [0, 1]

insert([1], 1, { items: [0] }) //=> [1, 0]

insert([1, 2, 3], 1, { items: [5] }) //=> [1, 5, 2, 3]

insert([1, 2, 3], -1, { items: [5] }) //=> [1, 2, 5, 3]

insert([1, 2, 3], -3, { items: [5] }) //=> [5, 1, 2, 3]

insert([1, 2, 3], 3, { items: [5] }) //=> [1, 2, 3, 5]

insert([1, 2, 3], 3, { items: [5, 6] }) //=> [1, 2, 3, 5, 6]

insert([1, 2, 3], null, { items: [5, 6] }) //=> [1, 2, 3, 5, 6]

insert([1, 2, 3], 0, { items: [[5, 6]] }) //=> [[5, 6], 1, 2, 3]

insert([1, 2, 3], 0, { items: [5, 6] }) //=> [5, 6, 1, 2, 3]

// Predicate-based insert
insert([1, 2, 3], (x) => x === 2, { items: [5] }) //=> [1, 5, 2, 3]

insert([1, 2, 3], (x) => x === 2, { items: [5], position: 'after' }) //=> [1, 2, 5, 3]

Try it

Last updated on
Edit this page