combinations es-toolkit
Returns all r-length combinations of elements from the input array.
Usage
combinations(arr, r)
Use combinations when you want every way to choose r items from an array, ignoring order. Tuples are emitted in lexicographic order based on the position of elements in the input array.
The total number of combinations is n! / r! / (n - r)! when 0 <= r <= n, and zero when r > n.
Elements are treated as unique by position, not by value, so duplicates in the input may produce combinations that look identical.
When r is 0, the result is a single empty combination. When r is greater than the array length, the result is an empty array.
Parameters
The array to choose from.
The length of each combination. Must be a non-negative integer.
Returns
An array of r-length combinations.
Throws
Throws an error if r is not a non-negative integer.
Try It
::: sandpack
:::
Source: es-toolkit
Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.