Enter a search term above to see results...
On This Page
Array Utilities
Functions for working with arrays.
filterEmpty
Remove undefined values from an array.
Syntax
filterEmpty(arr)Parameters
| Name | Type | Description |
|---|---|---|
| arr | array | The array to filter |
Returns
Array with undefined values removed.
Example
first
Get the first element(s) from an array.
Syntax
first(array, number = 1)Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| array | array | The input array | |
| number | number | The number of elements to return | 1 |
Returns
The first element, an array of the first n elements, or undefined if the array is empty.
Example
firstMatch
Return the first value that matches the callback condition.
Syntax
firstMatch(array, callback)Parameters
| Name | Type | Description |
|---|---|---|
| array | array | The input array |
| callback | function | The callback function to test each element |
Returns
The first matching element or undefined if no match is found.
Example
findIndex
Find the index of the first element that satisfies the testing function.
Syntax
findIndex(array, callback)Parameters
| Name | Type | Description |
|---|---|---|
| array | array | The input array |
| callback | function | The callback function to test each element |
Returns
The index of the first matching element, or -1 if no match is found.
Example
last
Get the last element(s) from an array.
Syntax
last(array, number = 1)Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| array | array | The input array | |
| number | number | The number of elements to return | 1 |
Returns
The last element, an array of the last n elements, or undefined if the array is empty.
Example
remove
Remove an element from the array that matches the callback or value.
Syntax
remove(array, callbackOrValue)Parameters
| Name | Type | Description |
|---|---|---|
| array | array | The input array |
| callbackOrValue | function or any | The callback function to test each element or the value to remove |
Returns
Count of removed elements.
Example
inArray
Check if a value is in the array.
Syntax
inArray(value, array = [])Parameters
| Name | Type | Description |
|---|---|---|
| value | any | The value to search for |
| array | array | The array to search in |
Returns
True if the value is in the array, false otherwise.
Example
range
Generate an array of numbers within a specified range.
Syntax
range(start, stop, step = 1)Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| start | number | The start of the range (or end if stop is not provided) | |
| stop | number | The end of the range (optional) | |
| step | number | The step between numbers | 1 |
Returns
Array of numbers within the specified range.
Example
moveItem
Move an item in an array to a specified index.
Syntax
moveItem(array = [], callbackOrValue, index)Parameters
| Name | Type | Description |
|---|---|---|
| array | array | The array to modify |
| callbackOrValue | function or any | The callback function to test each element or the value to move |
| index | number or string | The target index. Can be a number, ‘first’, or ‘last’ |
Returns
The modified array.
Example
moveToFront
Move an item to the front of an array.
Syntax
moveToFront(array = [], callbackOrValue)Parameters
| Name | Type | Description |
|---|---|---|
| array | array | The array to modify |
| callbackOrValue | function or any | The callback function to test each element or the value to move |
Returns
The modified array.
Example
moveToBack
Move an item to the back of an array.
Syntax
moveToBack(array = [], callbackOrValue)Parameters
| Name | Type | Description |
|---|---|---|
| array | array | The array to modify |
| callbackOrValue | function or any | The callback function to test each element or the value to move |
Returns
The modified array.
Example
sum
Calculate the sum of an array of numbers.
Syntax
sum(values)Parameters
| Name | Type | Description |
|---|---|---|
| values | array | An array of numbers |
Returns
The sum of all numbers in the array.
Example
unique
Remove duplicates from an array.
Syntax
unique(arr)Parameters
| Name | Type | Description |
|---|---|---|
| arr | array | The array to remove duplicates from |
Returns
Array with duplicates removed.
Example
where
Filter an array of objects based on matching properties.
Syntax
where(array, properties)Parameters
| Name | Type | Description |
|---|---|---|
| array | array | The array of objects to filter |
| properties | object | An object with properties to match |
Returns
Array of objects that match all the specified properties.
Example
flatten
Flatten a nested array structure.
Syntax
flatten(arr)Parameters
| Name | Type | Description |
|---|---|---|
| arr | array | The array to flatten |
Returns
A flattened array.
Example
some
Check if at least one element satisfies the predicate.
Syntax
some(collection, predicate)Parameters
| Name | Type | Description |
|---|---|---|
| collection | array | The collection to iterate over |
| predicate | function | The function invoked per iteration |
Returns
True if any element passes the predicate check, else false.
Example
any
An alias for some.
Example
sortBy
Sort an array of objects by one or more keys.
Syntax
sortBy(arr, key, comparator)Parameters
| Name | Type | Description |
|---|---|---|
| arr | array | The array to sort |
| key | string or array | The key to sort by, or array of keys for multi-key sorting |
| comparator | function | Optional custom comparison function |
Returns
A sorted array.
Example
groupBy
Group an array of objects by a specific property.
Syntax
groupBy(array, property)Parameters
| Name | Type | Description |
|---|---|---|
| array | array | The array to group |
| property | string | The property to group by |
Returns
Object where keys are distinct values of the property, and values are arrays of matching elements.
Example
intersection
Find elements common to all input arrays.
Syntax
intersection(...arrays)Parameters
| Name | Type | Description |
|---|---|---|
| arrays | …Array | Arrays to find common elements between |
Returns
Array containing elements present in all input arrays.
Example
difference
Find elements unique to the first array.
Syntax
difference(...arrays)Parameters
| Name | Type | Description |
|---|---|---|
| arrays | …Array | First array is filtered against all others |
Returns
Array containing elements present in first array but not in any other input arrays.
Example
uniqueItems
Find elements that appear in exactly one input array.
Syntax
uniqueItems(...arrays)Parameters
| Name | Type | Description |
|---|---|---|
| arrays | …Array | Arrays to compare |
Returns
Array containing elements that appear in exactly one of the input arrays.