Function fill

  • Fills the whole array with a specified value.

    This function mutates the original array and replaces its elements with the provided value, starting from the specified start index up to the end index (non-inclusive). If the start or end indices are not provided, it defaults to filling the entire array.

    Type Parameters

    • T

    Parameters

    • array: unknown[]

      The array to fill.

    • value: T

      The value to fill the array with.

    Returns T[]

    The array with the filled values.

    const array = [1, 2, 3];
    const result = fill(array, 'a');
    // => ['a', 'a', 'a']

    const result = fill(Array(3), 2);
    // => [2, 2, 2]

    const result = fill([4, 6, 8, 10], '*', 1, 3);
    // => [4, '*', '*', 10]

    const result = fill(array, '*', -2, -1);
    // => [1, '*', 3]
  • Fills elements of an array with a specified value from the start position up to the end of the array.

    This function mutates the original array and replaces its elements with the provided value, starting from the specified start index up to the end index (non-inclusive). If the start or end indices are not provided, it defaults to filling the entire array.

    Type Parameters

    • T
    • U

    Parameters

    • array: (T | U)[]

      The array to fill.

    • value: U

      The value to fill the array with.

    • Optionalstart: number

      The start position. Defaults to 0.

    Returns (T | U)[]

    The array with the filled values.

    const array = [1, 2, 3];
    const result = fill(array, 'a');
    // => ['a', 'a', 'a']

    const result = fill(Array(3), 2);
    // => [2, 2, 2]

    const result = fill([4, 6, 8, 10], '*', 1, 3);
    // => [4, '*', '*', 10]

    const result = fill(array, '*', -2, -1);
    // => [1, '*', 3]
  • Fills elements of an array with a specified value from the start position up to, but not including, the end position.

    This function mutates the original array and replaces its elements with the provided value, starting from the specified start index up to the end index (non-inclusive). If the start or end indices are not provided, it defaults to filling the entire array.

    Type Parameters

    • T
    • U

    Parameters

    • array: (T | U)[]

      The array to fill.

    • value: U

      The value to fill the array with.

    • Optionalstart: number

      The start position. Defaults to 0.

    • Optionalend: number

      The end position. Defaults to the array's length.

    Returns (T | U)[]

    The array with the filled values.

    const array = [1, 2, 3];
    const result = fill(array, 'a');
    // => ['a', 'a', 'a']

    const result = fill(Array(3), 2);
    // => [2, 2, 2]

    const result = fill([4, 6, 8, 10], '*', 1, 3);
    // => [4, '*', '*', 10]

    const result = fill(array, '*', -2, -1);
    // => [1, '*', 3]