Array Helpers
Utility functions for working with array operations.
Functions
Section titled “Functions”| Function | Description |
|---|---|
cartesianProduct | Computes the Cartesian product of the provided arrays. |
chunk | Chunks an array into smaller arrays of specified size |
compact | Removes all falsy values (false, null, undefined, 0, "", NaN) from an array. |
countBy | Groups the elements of an array by the key returned by keyFn and returns a record mapping each key to the number of… |
createSortByDateFn | Creates a sort function for objects by date property. |
createSortByNaturalFn | Creates a sort function for objects by one or more string properties using natural ordering. |
createSortByNumberFn | Creates a sort function for objects by number property. |
createSortByStringFn | Creates a sort function for objects by one or more string properties. |
difference | Returns the difference between two arrays (items in first array but not in second) |
drop | native JS Array.prototype.slice(n) (ES3) |
ensureArray | Wraps a value in an array if it is not already one. |
equalsDeep | Recursive structural array equality. |
equalsShallow | Positional, one-level (shallow) array equality. |
equalsUnordered | Order-independent (set-style) array equality. |
find / findIndex | native JS Array.prototype.find() / findIndex() (ES2015) |
flatten / flat | native JS Array.prototype.flat(depth?) (ES2019) |
groupBy / group | native JS Object.groupBy(arr, fn) (ES2024) |
head / first | native JS Array.prototype.at(0) (ES2022) |
includes | native JS Array.prototype.includes() (ES2016) |
intersection | Compute the intersection of two arrays, meaning the elements that are present in both arrays. |
intersects | Simple helper that check if two lists shared at least an item in common. |
isEmpty | Checks if an array is empty (has no elements). |
isNonEmpty | Checks if an array is non-empty (has at least one element). |
last | native JS Array.prototype.at(-1) (ES2022) |
max | Returns the maximum value in an array using a loop instead of spread, avoiding the call stack overflow that occurs wi… |
min | Returns the minimum value in an array using a loop instead of spread, avoiding the call stack overflow that occurs wi… |
partition | Splits an array into two groups based on a predicate function. |
range | Generates an array of sequential numbers from start to end (exclusive). |
reverse | native JS Array.prototype.toReversed() (ES2023) |
sample | Picks one or more random elements from an array. |
select | Filters and transforms an array in a single pass. |
select / filterMap | native JS Array.prototype.filter().map() (ES5) |
shuffle | Randomly reorders elements of an array using the Fisher-Yates algorithm. |
sort (basic comparator) | native JS (a, b) => a - b / a.localeCompare(b) (ES1) |
sortBy / orderBy | native JS Array.prototype.toSorted(fn?) (ES2023) |
sortNumberAscFn | Sort numbers in ascending order |
sortNumberDescFn | Sort numbers in descending order |
sortStringAscFn | Sort strings in ascending order |
sortStringAscInsensitiveFn | Sort strings in ascending order (case insensitive) |
sortStringDescFn | Sort strings in descending order |
sortStringNaturalAscFn | Sort strings in ascending order using natural (human-friendly) ordering. |
sortStringNaturalAscInsensitiveFn | Sort strings in ascending natural order (case insensitive). |
sortStringNaturalDescFn | Sort strings in descending order using natural (human-friendly) ordering. |
sortStringNaturalDescInsensitiveFn | Sort strings in descending natural order (case insensitive). |
tail | native JS Array.prototype.slice(1) (ES3) |
take | native JS Array.prototype.slice(0, n) (ES3) |
union | native JS unique([...a, ...b]) (ES2015) |
unique | Removes duplicate values from an array |
unzip | Splits an array of tuples into separate arrays, one per position. |
without | Returns a new array with all occurrences of the given values removed. |
zip | Combines multiple arrays element-by-element into an array of tuples. |