Function zip

  • Builds N-tuples of elements from the given N arrays with matching indices, stopping when the smallest array's end is reached.

    Type Parameters

    • T extends unknown[]

      the type of the tuples produced by this function.

    Parameters

    • Rest...arrays: {
          [K in string | number | symbol]: readonly T[K<K>][]
      }

      The arrays to zip.

    Returns T[]

    A new array containing N-tuples of elements from the given arrays.

    import { zip } from "@std/collections/zip";
    import { assertEquals } from "@std/assert";

    const numbers = [1, 2, 3, 4];
    const letters = ["a", "b", "c", "d"];
    const pairs = zip(numbers, letters);

    assertEquals(
    pairs,
    [
    [1, "a"],
    [2, "b"],
    [3, "c"],
    [4, "d"],
    ],
    );