Enter a search term above to see results...
On This Page
Array Utilities
The Array utilities provide a set of functions for working with arrays in JavaScript. These functions help in manipulating, transforming, and querying arrays efficiently.
Functions
unique
Remove duplicates from an array.
Parameters
Name | Type | Description |
---|---|---|
arr | array | The array to remove duplicates from |
Returns
A new array with duplicates removed.
Example
filterEmpty
Remove undefined values from an array.
Parameters
Name | Type | Description |
---|---|---|
arr | array | The array to filter |
Returns
A new array with undefined values removed.
Example
last
Get the last element(s) from an array.
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
first
Get the first element(s) from an array.
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
Iterate through an array and return the first value that matches the callback condition.
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 in the array that satisfies the provided testing function.
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
remove
Remove an element from the array that matches the provided callback or value.
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
True if an element was removed, false otherwise.
Example
inArray
Check if a value is in the 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.
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
An array of numbers within the specified range.
Example
moveItem
Move an item in an array to a specified index based on a callback or value match.
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 to move the item to. Can be a number, ‘first’ (equivalent to 0), or ‘last’ (equivalent to array length - 1) |
Returns
The modified array.
Example
moveToFront
Move an item to the front of an array based on a callback or value match.
Equivalent to moveItem(array, callbackOrValue, 'first')
.
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 based on a callback or value match.
Equivalent to moveItem(array, callbackOrValue, 'last')
.
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.
Parameters
Name | Type | Description |
---|---|---|
values | array | An array of numbers |
Returns
The sum of all numbers in the array.
Example
where
Filter an array of objects based on matching properties.
Parameters
Name | Type | Description |
---|---|---|
array | array | The array of objects to filter |
properties | object | An object with properties to match |
Returns
An array of objects that match all the specified properties.
Example
flatten
Flatten a nested array structure.
Parameters
Name | Type | Description |
---|---|---|
arr | array | The array to flatten |
Returns
A new, flattened array.
Example
some
Check if at least one element in the collection satisfies the 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
.
sortBy
Sort an array of objects by a specific key.
Parameters
Name | Type | Description |
---|---|---|
arr | array | The array to sort |
key | string | The key to sort by |
comparator | function | Optional custom comparison function |
Returns
A new sorted array.
Example
groupBy
Group an array of objects by a specific property.
Parameters
Name | Type | Description |
---|---|---|
array | array | The array to group |
property | string | The property to group by |
Returns
An object where keys are distinct values of the specified property, and values are arrays of elements that have that property value.