Function zipWith

  • Combines multiple arrays into a single array using a custom combiner function.

    This function takes multiple arrays and a combiner function, and returns a new array where each element is the result of applying the combiner function to the corresponding elements of the input arrays.

    Type Parameters

    • T

      The type of elements in the first array.

    • R

      The type of elements in the resulting array.

    Parameters

    • arr1: readonly T[]

      The first array to zip.

    • combine: ((item: T) => R)

      The combiner function that takes corresponding elements from each array and returns a single value.

        • (item): R
        • Parameters

          • item: T

          Returns R

    Returns R[]

    A new array where each element is the result of applying the combiner function to the corresponding elements of the input arrays.

    // Example usage with two arrays:
    const arr1 = [1, 2, 3];
    const arr2 = [4, 5, 6];
    const result = zipWith(arr1, arr2, (a, b) => a + b);
    // result will be [5, 7, 9]
    // Example usage with three arrays:
    const arr1 = [1, 2];
    const arr2 = [3, 4];
    const arr3 = [5, 6];
    const result = zipWith(arr1, arr2, arr3, (a, b, c) => `${a}${b}${c}`);
    // result will be [`135`, `246`]
  • Combines two arrays into a single array using a custom combiner function.

    Type Parameters

    • T

      The type of elements in the first array.

    • U

      The type of elements in the second array.

    • R

      The type of elements in the resulting array.

    Parameters

    • arr1: readonly T[]

      The first array to zip.

    • arr2: readonly U[]

      The second array to zip.

    • combine: ((item1: T, item2: U) => R)

      The combiner function that takes corresponding elements from each array and returns a single value.

        • (item1, item2): R
        • Parameters

          • item1: T
          • item2: U

          Returns R

    Returns R[]

    A new array where each element is the result of applying the combiner function to the corresponding elements of the input arrays.

  • Combines three arrays into a single array using a custom combiner function.

    Type Parameters

    • T

      The type of elements in the first array.

    • U

      The type of elements in the second array.

    • V

      The type of elements in the third array.

    • R

      The type of elements in the resulting array.

    Parameters

    • arr1: readonly T[]

      The first array to zip.

    • arr2: readonly U[]

      The second array to zip.

    • arr3: readonly V[]

      The third array to zip.

    • combine: ((item1: T, item2: U, item3: V) => R)

      The combiner function that takes corresponding elements from each array and returns a single value.

        • (item1, item2, item3): R
        • Parameters

          • item1: T
          • item2: U
          • item3: V

          Returns R

    Returns R[]

    A new array where each element is the result of applying the combiner function to the corresponding elements of the input arrays.

  • Combines four arrays into a single array using a custom combiner function.

    Type Parameters

    • T

      The type of elements in the first array.

    • U

      The type of elements in the second array.

    • V

      The type of elements in the third array.

    • W

      The type of elements in the fourth array.

    • R

      The type of elements in the resulting array.

    Parameters

    • arr1: readonly T[]

      The first array to zip.

    • arr2: readonly U[]

      The second array to zip.

    • arr3: readonly V[]

      The third array to zip.

    • arr4: readonly W[]

      The fourth array to zip.

    • combine: ((item1: T, item2: U, item3: V, item4: W) => R)

      The combiner function that takes corresponding elements from each array and returns a single value.

        • (item1, item2, item3, item4): R
        • Parameters

          • item1: T
          • item2: U
          • item3: V
          • item4: W

          Returns R

    Returns R[]

    A new array where each element is the result of applying the combiner function to the corresponding elements of the input arrays.