Function unzip

  • Builds two separate arrays from the given array of 2-tuples, with the first returned array holding all first tuple elements and the second one holding all the second elements.

    Type Parameters

    • T

      The type of the first tuple elements.

    • U

      The type of the second tuple elements.

    Parameters

    • pairs: readonly [T, U][]

      The array of 2-tuples to unzip.

    Returns [T[], U[]]

    A tuple containing two arrays, the first one holding all first tuple elements and the second one holding all second elements.

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

    const parents = [
    ["Maria", "Jeff"],
    ["Anna", "Kim"],
    ["John", "Leroy"],
    ] as [string, string][];

    const [moms, dads] = unzip(parents);

    assertEquals(moms, ["Maria", "Anna", "John"]);
    assertEquals(dads, ["Jeff", "Kim", "Leroy"]);