Skip to content

Array Helpers

Utility functions for working with array operations.

FunctionDescription
cartesianProductComputes the Cartesian product of the provided arrays.
chunkChunks an array into smaller arrays of specified size
compactRemoves all falsy values (false, null, undefined, 0, "", NaN) from an array.
countByGroups the elements of an array by the key returned by keyFn and returns a record mapping each key to the number of…
createSortByDateFnCreates a sort function for objects by date property.
createSortByNaturalFnCreates a sort function for objects by one or more string properties using natural ordering.
createSortByNumberFnCreates a sort function for objects by number property.
createSortByStringFnCreates a sort function for objects by one or more string properties.
differenceReturns the difference between two arrays (items in first array but not in second)
dropnative JS Array.prototype.slice(n) (ES3)
ensureArrayWraps a value in an array if it is not already one.
equalsDeepRecursive structural array equality.
equalsShallowPositional, one-level (shallow) array equality.
equalsUnorderedOrder-independent (set-style) array equality.
find / findIndexnative JS Array.prototype.find() / findIndex() (ES2015)
flatten / flatnative JS Array.prototype.flat(depth?) (ES2019)
groupBy / groupnative JS Object.groupBy(arr, fn) (ES2024)
head / firstnative JS Array.prototype.at(0) (ES2022)
includesnative JS Array.prototype.includes() (ES2016)
intersectionCompute the intersection of two arrays, meaning the elements that are present in both arrays.
intersectsSimple helper that check if two lists shared at least an item in common.
isEmptyChecks if an array is empty (has no elements).
isNonEmptyChecks if an array is non-empty (has at least one element).
lastnative JS Array.prototype.at(-1) (ES2022)
maxReturns the maximum value in an array using a loop instead of spread, avoiding the call stack overflow that occurs wi…
minReturns the minimum value in an array using a loop instead of spread, avoiding the call stack overflow that occurs wi…
partitionSplits an array into two groups based on a predicate function.
rangeGenerates an array of sequential numbers from start to end (exclusive).
reversenative JS Array.prototype.toReversed() (ES2023)
samplePicks one or more random elements from an array.
selectFilters and transforms an array in a single pass.
select / filterMapnative JS Array.prototype.filter().map() (ES5)
shuffleRandomly reorders elements of an array using the Fisher-Yates algorithm.
sort (basic comparator)native JS (a, b) => a - b / a.localeCompare(b) (ES1)
sortBy / orderBynative JS Array.prototype.toSorted(fn?) (ES2023)
sortNumberAscFnSort numbers in ascending order
sortNumberDescFnSort numbers in descending order
sortStringAscFnSort strings in ascending order
sortStringAscInsensitiveFnSort strings in ascending order (case insensitive)
sortStringDescFnSort strings in descending order
sortStringNaturalAscFnSort strings in ascending order using natural (human-friendly) ordering.
sortStringNaturalAscInsensitiveFnSort strings in ascending natural order (case insensitive).
sortStringNaturalDescFnSort strings in descending order using natural (human-friendly) ordering.
sortStringNaturalDescInsensitiveFnSort strings in descending natural order (case insensitive).
tailnative JS Array.prototype.slice(1) (ES3)
takenative JS Array.prototype.slice(0, n) (ES3)
unionnative JS unique([...a, ...b]) (ES2015)
uniqueRemoves duplicate values from an array
unzipSplits an array of tuples into separate arrays, one per position.
withoutReturns a new array with all occurrences of the given values removed.
zipCombines multiple arrays element-by-element into an array of tuples.