Interface IArrayExtensions<TSource>

interface IArrayExtensions<TSource> {
    clear(): void;
    insert(index: number, item: TSource): void;
    insertRange(index: number, collection: Iterable<TSource>): void;
    remove(item: TSource): boolean;
    removeAll(predicate: (item: TSource, index: number) => boolean): number;
}

Type Parameters

  • TSource

Methods

  • Removes all items from the array. Original array is mutated.

    Returns void

    const arr = [1, 2, 3];
    arr.clear(); // arr is now []
  • Inserts an item at the given index into the array. Existing items at and after the index will be pushed back. No items will be deleted. Original array is mutated.

    Parameters

    • index: number

      The index of which to insert the item.

    • item: TSource

      The item to insert.

    Returns void

    const arr = [1, 2, 3];
    arr.insert(1, 5); // arr is now [1, 5, 2, 3]
  • Inserts an item at the given index into the array. Existing items at and after the index will be pushed back. No items will be deleted. Original array is mutated. *

    Parameters

    • index: number

      The index of which to start the insertion.

    • collection: Iterable<TSource>

      The items to insert.

    Returns void

    const arr = [1, 2, 3];
    arr.insertRange(1, [5, 6]); // arr is now [1, 5, 6, 2, 3]
  • Removes an item from the array. Original array is mutated.

    Parameters

    • item: TSource

      Item to remove from array.

    Returns boolean

    true if item was found and removed from array. False if item was not found in the array.

    const numbers = [1, 2, 3];
    const res = numbers.remove(2); // numbers is modified in place. res is true because element was in the array.
  • Removes all items from the array that pass the predicate. Original array is mutated.

    Parameters

    • predicate: (item: TSource, index: number) => boolean

      The predicate to test for a condition.

    Returns number

    The number of removed items.

    const arr = [1, 2, 3, 4];
    arr.removeAll(x => x > 2); // arr is now [1, 2]