Array
arrayRemove

📤 Removes items at the specified index from an array.

Immutable: This function does not mutate the original array.

Syntax

import { arrayRemove } from '@opentf/std';
 
arrayRemove<T>(
  arr: T[] = [],
  index?: number,
  count: number = 1
): T[]

Parameters

  • arr: The source array.
  • index: The index from which to start removing items. If not provided, it removes the last item.
  • count: The number of items to remove. Defaults to 1.

Returns

A new array with the items removed.

Examples

arrayRemove([]) //=> []
 
arrayRemove([1]) //=> []
 
arrayRemove([1, 2]) //=> [1]
 
arrayRemove([1, 2], 1) //=> [1]
 
arrayRemove([1, 2], 1, 1) //=> [1]
 
arrayRemove([1, 2, 3, 4, 5], -1) //=> [1, 2, 3, 4]
 
arrayRemove([1, 2, 3, 4, 5], -1, 1) //=> [1, 2, 3, 4]
 
arrayRemove([1, 2, 3, 4, 5], -1, 3) //=> [1, 2, 3, 4]
 
arrayRemove([1, 2, 3, 4, 5], -2, 2) //=> [1, 2, 3]
 
const myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];
arrayRemove(myFish, 2, Infinity) //=> ['angel', 'clown']

Related

Try