@oazmi/kitchensink - v0.9.13
    Preparing search index...

    Function zipArrays

    • zip together a list of input arrays as tuples, similar to python's zip function.

      Note

      if one of the input arrays is shorter in length than all the other input arrays, then this zip function will only generate tuples up until the shortest array is expended, similar to how python's zip function behaves. in a sense, this feature is what sets it apart from the 2d array transpose function transposeArray2D, which decides its output length based on the first array's length.

      Tip

      applying the zip function twice will give you back the original arrays (assuming they all had the same length). so in a sense, to unzip the output of zipArrays, you simply apply zipArrays to again (after performing an array spread operation).

      Important

      this function only accepts array inputs to zip, and not iterators. to zip a sequence of iterators, use the zipIterators function (which has a slightly slower performance).

      Type Parameters

      • T extends any[]

      Parameters

      • ...arrays: any[][]

      Returns T[]

      import { assertEquals } from "jsr:@std/assert"

      type MyObj = { key: string }
      type MyTuple = [number, boolean, MyObj]

      const
      my_num_arr: number[] = [100, 101, 102, 103, 104],
      my_bool_arr: boolean[] = [true, false, false, true, false],
      my_obj_arr: MyObj[] = [{ key: "a" }, { key: "b" }, { key: "c" }, { key: "d" }]
      // notice that `my_obj_arr` is shorter than the other two arrays. (i.e. has a length of `4`, while others are `5`)
      // this would mean that zipping them together would only generate a 3-tuple array of `4` elements.

      const my_tuples_arr: MyTuple[] = zipArrays<[number, boolean, MyObj]>(my_num_arr, my_bool_arr, my_obj_arr)
      assertEquals(my_tuples_arr, [
      [100, true, { key: "a" }],
      [101, false, { key: "b" }],
      [102, false, { key: "c" }],
      [103, true, { key: "d" }],
      ])

      // to unzip the array of tuples, and receive back the original (trimmed) arrays, simply apply `zipArrays` again.
      const my_arrs = [
      [ 1, 2, 3, 4],
      [true, false, true, true],
      [ "w", "x", "y", "z"],
      ]
      assertEquals(zipArrays(...zipArrays(...my_arrs)), my_arrs)

      // zipping no input arrays should not iterate infinitely.
      assertEquals(zipArrays(), [])