Function overwrite

  • Category

    Overwrite elements in an array at a given index.

    Time complexity: O(n)

    Space complexity: O(n)

    Type Parameters

    • T

    Parameters

    • array: T[]

      The array to overwrite elements in

    • startIndex: number

      The index to start overwriting elements from

    • data: ArrayLike<T> & Iterable<T>

    Returns ReturnType<typeof array["splice"]>

    The return value of Array.prototype.splice

    Array

    const array = [1, 2, 3, 4, 5];

    overwrite(array, 1, [8, 9]); // [2, 3]

    console.log(array); // [1, 8, 9, 4, 5]