Array/Since
1.38.0unzip
Separates an array of pairs into two arrays containing the first and second values respectively.
unzip(array):[T[], U[]]Parameters
array[T, U][]requiredAn array of two-element tuples to separate into two arrays.
Returns
[T[], U[]]A tuple containing two arrays: the first contains all first values, and the second contains all second values.
Examples
01
Basic usage
Separate pairs of numbers into two arrays.
TypeScript
import { unzip } from "@cyref/js";
const pairs: [number, number][] = [
[1, 10],
[2, 20],
[3, 30],
];
const result = unzip(pairs);
console.log(result);
// [
// [1, 2, 3],
// [10, 20, 30],
// ]02
With different types
The two values in each pair can have different types.
TypeScript
import { unzip } from "@cyref/js";
const pairs: [string, number][] = [
["Amir", 20],
["Sara", 25],
["Reza", 30],
];
const result = unzip(pairs);
console.log(result);
// [
// ["Amir", "Sara", "Reza"],
// [20, 25, 30],
// ]03
With objects
Tuple values can also contain objects.
TypeScript
import { unzip } from "@cyref/js";
const pairs: [string, { active: boolean }][] = [
["Amir", { active: true }],
["Sara", { active: false }],
];
const result = unzip(pairs);
console.log(result);
// [
// ["Amir", "Sara"],
// [
// { active: true },
// { active: false },
// ],
// ]04
Empty array
An empty array produces two empty arrays.
TypeScript
import { unzip } from "@cyref/js";
const result = unzip([]);
console.log(result);
// [[], []]Built with@cyref/js·Official documentation