Array Utilities API reference for array manipulation functions list API Reference
Categories

Array Utilities

Functions for working with arrays.

filterEmpty

Remove undefined values from an array.

Syntax

filterEmpty(arr)

Parameters

NameTypeDescription
arrarrayThe 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

NameTypeDescriptionDefault
arrayarrayThe input array
numbernumberThe number of elements to return1

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

NameTypeDescription
arrayarrayThe input array
callbackfunctionThe 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

NameTypeDescription
arrayarrayThe input array
callbackfunctionThe 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

NameTypeDescriptionDefault
arrayarrayThe input array
numbernumberThe number of elements to return1

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

NameTypeDescription
arrayarrayThe input array
callbackOrValuefunction or anyThe 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

NameTypeDescription
valueanyThe value to search for
arrayarrayThe 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

NameTypeDescriptionDefault
startnumberThe start of the range (or end if stop is not provided)
stopnumberThe end of the range (optional)
stepnumberThe step between numbers1

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

NameTypeDescription
arrayarrayThe array to modify
callbackOrValuefunction or anyThe callback function to test each element or the value to move
indexnumber or stringThe 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

NameTypeDescription
arrayarrayThe array to modify
callbackOrValuefunction or anyThe 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

NameTypeDescription
arrayarrayThe array to modify
callbackOrValuefunction or anyThe 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

NameTypeDescription
valuesarrayAn array of numbers

Returns

The sum of all numbers in the array.

Example

unique

Remove duplicates from an array.

Syntax

unique(arr)

Parameters

NameTypeDescription
arrarrayThe 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

NameTypeDescription
arrayarrayThe array of objects to filter
propertiesobjectAn 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

NameTypeDescription
arrarrayThe array to flatten

Returns

A flattened array.

Example

some

Check if at least one element satisfies the predicate.

Syntax

some(collection, predicate)

Parameters

NameTypeDescription
collectionarrayThe collection to iterate over
predicatefunctionThe 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

NameTypeDescription
arrarrayThe array to sort
keystring or arrayThe key to sort by, or array of keys for multi-key sorting
comparatorfunctionOptional custom comparison function

Returns

A sorted array.

Example

groupBy

Group an array of objects by a specific property.

Syntax

groupBy(array, property)

Parameters

NameTypeDescription
arrayarrayThe array to group
propertystringThe 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

NameTypeDescription
arrays…ArrayArrays 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

NameTypeDescription
arrays…ArrayFirst 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

NameTypeDescription
arrays…ArrayArrays to compare

Returns

Array containing elements that appear in exactly one of the input arrays.

Example

Previous
Utils
Next
Browser