Overview
TheArrayUtils class provides a comprehensive set of static methods for array manipulation, including chunking, flattening, deduplication, shuffling, set operations, and statistical functions.
Source: workspace/source/src/utils/helpers/ArrayUtils.ts:5
Methods
chunk
Split array into chunks of specified size.arr: T[]- The array to splitsize: number- Size of each chunk (must be greater than 0)
T[][] - Array of chunks
Throws: Error if chunk size is less than or equal to 0
Example:
flatten
Flatten nested arrays to a specified depth.arr: Array<T | T[]>- The array to flattendepth: number- Depth level to flatten (default: 1)
T[] - Flattened array
Example:
unique
Get unique values from array, optionally using a key function.arr: T[]- The array to deduplicateby?: (item: T) => unknown- Optional function to extract comparison key
T[] - Array with unique values
Example:
deduplicate
Remove duplicates and return unique array (alias forunique).
arr: T[]- The array to deduplicate
T[] - Array with unique values
compact
Remove falsy values (null, undefined, false, 0, empty string) from array.arr: Array<T | null | undefined | false | 0 | "">- The array to compact
T[] - Array with truthy values only
Example:
flat
Flatten array one level deep.arr: Array<T | T[]>- The array to flatten
T[] - Array flattened one level
Example:
first
Get the first element of an array.arr: T[]- The array
T | undefined - First element or undefined if array is empty
Example:
last
Get the last element of an array.arr: T[]- The array
T | undefined - Last element or undefined if array is empty
Example:
at
Get element at index (supports negative indices).arr: T[]- The arrayindex: number- The index (negative indices count from end)
T | undefined - Element at index or undefined
Example:
shuffle
Randomly shuffle array elements using Fisher-Yates algorithm.arr: T[]- The array to shuffle
T[] - New shuffled array (original is not modified)
Example:
random
Get a random element from array.arr: T[]- The array
T | undefined - Random element or undefined if array is empty
Example:
randomN
Get N random elements from array.arr: T[]- The arrayn: number- Number of random elements to get
T[] - Array of N random elements
Example:
reverse
Reverse array elements.arr: T[]- The array to reverse
T[] - New reversed array (original is not modified)
Example:
rotate
Rotate array elements by specified steps.arr: T[]- The array to rotatesteps: number- Number of positions to rotate (positive = right, negative = left)
T[] - New rotated array
Example:
zip
Zip multiple arrays together into tuples....arrays: T[][]- Arrays to zip together
T[][] - Array of tuples
Example:
unzip
Unzip array of tuples into separate arrays.arr: T[][]- Array of tuples to unzip
T[][] - Array of separate arrays
Example:
difference
Find elements in first array that are not in second array.arr1: T[]- First arrayarr2: T[]- Second array
T[] - Elements unique to first array
Example:
intersection
Find common elements between two arrays.arr1: T[]- First arrayarr2: T[]- Second array
T[] - Common elements
Example:
union
Find all unique elements from both arrays.arr1: T[]- First arrayarr2: T[]- Second array
T[] - Combined unique elements
Example:
includesAll
Check if array includes all elements from another array.arr: T[]- The array to checkitems: T[]- Elements to look for
boolean - True if all items are present
Example:
includesAny
Check if array includes any element from another array.arr: T[]- The array to checkitems: T[]- Elements to look for
boolean - True if any item is present
Example:
sum
Calculate sum of all numbers in array.arr: number[]- Array of numbers
number - Sum of all numbers
Example:
average
Calculate average (mean) of numbers in array.arr: number[]- Array of numbers
number - Average value (0 if array is empty)
Example:
min
Find minimum value in array.arr: number[]- Array of numbers
number | undefined - Minimum value or undefined if empty
Example:
max
Find maximum value in array.arr: number[]- Array of numbers
number | undefined - Maximum value or undefined if empty
Example:
range
Generate array of numbers from start to end (exclusive).start: number- Starting numberend: number- Ending number (exclusive)step: number- Step increment (default: 1)
number[] - Array of numbers
Example:
repeat
Repeat array elements N times.arr: T[]- The array to repeattimes: number- Number of repetitions
T[] - Repeated array
Example:
fill
Create array filled with specified value.length: number- Length of arrayvalue: T- Value to fill with
T[] - Filled array
Example:
transpose
Transpose 2D array (swap rows and columns).arr: T[][]- 2D array to transpose
T[][] - Transposed array
Example:
findIndex
Find index of element matching predicate.arr: T[]- The array to searchpredicate: (item: T, index: number) => boolean- Function to test elements
number - Index of first match or -1
Example:
findLastIndex
Find last index of element matching predicate.arr: T[]- The array to searchpredicate: (item: T, index: number) => boolean- Function to test elements
number - Index of last match or -1
Example:
partition
Partition array into two arrays based on predicate.arr: T[]- The array to partitionpredicate: (item: T) => boolean- Function to test elements
[T[], T[]] - Tuple of [passing elements, failing elements]
Example: