![Logo](/_astro/logo.DNC1PCTe_Z1abT04.webp)
Enter a search term above to see results...
Enter a search term above to see results...
The Array utilities provide a set of functions for working with arrays in JavaScript. These functions help in manipulating, transforming, and querying arrays efficiently.
function unique(arr)
Remove duplicates from an array.
Name | Type | Description |
---|---|---|
arr | array | The array to remove duplicates from |
A new array with duplicates removed.
import { unique } from '@semantic-ui/utils';
const arr = [1, 2, 2, 3, 4, 4, 5];console.log(unique(arr)); // [1, 2, 3, 4, 5]
function filterEmpty(arr)
Remove undefined values from an array.
Name | Type | Description |
---|---|---|
arr | array | The array to filter |
A new array with undefined values removed.
import { filterEmpty } from '@semantic-ui/utils';
const arr = [1, undefined, 2, null, 3, '', 4];console.log(filterEmpty(arr)); // [1, 2, null, 3, '', 4]
function last(array, number = 1)
Get the last element(s) from an array.
Name | Type | Description | Default |
---|---|---|---|
array | array | The input array | |
number | number | The number of elements to return | 1 |
The last element, an array of the last n elements, or undefined if the array is empty.
import { last } from '@semantic-ui/utils';
const arr = [1, 2, 3, 4, 5];console.log(last(arr)); // 5console.log(last(arr, 2)); // [4, 5]
function first(array, number = 1)
Get the first element(s) from an array.
Name | Type | Description | Default |
---|---|---|---|
array | array | The input array | |
number | number | The number of elements to return | 1 |
The first element, an array of the first n elements, or undefined if the array is empty.
import { first } from '@semantic-ui/utils';
const arr = [1, 2, 3, 4, 5];console.log(first(arr)); // 1console.log(first(arr, 2)); // [1, 2]
function firstMatch(array, callback)
Iterate through an array and return the first value that matches the callback condition.
Name | Type | Description |
---|---|---|
array | array | The input array |
callback | function | The callback function to test each element |
The first matching element or undefined if no match is found.
import { firstMatch } from '@semantic-ui/utils';
const arr = [1, 2, 3, 4, 5];const result = firstMatch(arr, (value) => value > 3);console.log(result); // 4
function findIndex(array, callback)
Find the index of the first element in the array that satisfies the provided testing function.
Name | Type | Description |
---|---|---|
array | array | The input array |
callback | function | The callback function to test each element |
The index of the first matching element, or -1 if no match is found.
import { findIndex } from '@semantic-ui/utils';
const arr = [1, 2, 3, 4, 5];const index = findIndex(arr, (value) => value > 3);console.log(index); // 3
function remove(array, callbackOrValue)
Remove an element from the array that matches the provided callback or value.
Name | Type | Description |
---|---|---|
array | array | The input array |
callbackOrValue | function or any | The callback function to test each element or the value to remove |
True if an element was removed, false otherwise.
import { remove } from '@semantic-ui/utils';
const arr = [1, 2, 3, 4, 5];remove(arr, 3);console.log(arr); // [1, 2, 4, 5]
remove(arr, (value) => value > 4);console.log(arr); // [1, 2, 4]
function inArray(value, array = [])
Check if a value is in the array.
Name | Type | Description |
---|---|---|
value | any | The value to search for |
array | array | The array to search in |
True if the value is in the array, false otherwise.
import { inArray } from '@semantic-ui/utils';
const arr = [1, 2, 3, 4, 5];console.log(inArray(3, arr)); // trueconsole.log(inArray(6, arr)); // false
function range(start, stop, step = 1)
Generate an array of numbers within a specified range.
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 |
An array of numbers within the specified range.
import { range } from '@semantic-ui/utils';
console.log(range(5)); // [0, 1, 2, 3, 4]console.log(range(1, 5)); // [1, 2, 3, 4]console.log(range(0, 10, 2)); // [0, 2, 4, 6, 8]
function moveItem(array = [], callbackOrValue, index)
Move an item in an array to a specified index based on a callback or value match.
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) |
The modified array.
import { moveItem } from '@semantic-ui/utils';
const arr = [1, 2, 3, 4];console.log(moveItem(arr, 3, 0)); // [3, 1, 2, 4]console.log(moveItem(arr, 2, 'last')); // [1, 3, 4, 2]console.log(moveItem(arr, x => x === 4, 'first')); // [4, 1, 2, 3]console.log(moveItem(arr, 2, 1)); // [1, 2, 3, 4]
function moveToFront(array = [], callbackOrValue)
Move an item to the front of an array based on a callback or value match.
Equivalent to moveItem(array, callbackOrValue, 'first')
.
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 |
The modified array.
import { moveToFront } from '@semantic-ui/utils';
const arr = [1, 2, 3, 4];console.log(moveToFront(arr, 3)); // [3, 1, 2, 4]
function moveToBack(array = [], callbackOrValue)
Move an item to the back of an array based on a callback or value match.
Equivalent to moveItem(array, callbackOrValue, 'last')
.
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 |
The modified array.
import { moveToBack } from '@semantic-ui/utils';
const arr = [1, 2, 3, 4];console.log(moveToBack(arr, 2)); // [1, 3, 4, 2]
function sum(values)
Calculate the sum of an array of numbers.
Name | Type | Description |
---|---|---|
values | array | An array of numbers |
The sum of all numbers in the array.
import { sum } from '@semantic-ui/utils';
const numbers = [1, 2, 3, 4, 5];console.log(sum(numbers)); // 15
function where(array, properties)
Filter an array of objects based on matching properties.
Name | Type | Description |
---|---|---|
array | array | The array of objects to filter |
properties | object | An object with properties to match |
An array of objects that match all the specified properties.
import { where } from '@semantic-ui/utils';
const people = [ { name: 'John', age: 30 }, { name: 'Jane', age: 25 }, { name: 'Bob', age: 30 }];const result = where(people, { age: 30 });console.log(result); // [{ name: 'John', age: 30 }, { name: 'Bob', age: 30 }]
function flatten(arr)
Flatten a nested array structure.
Name | Type | Description |
---|---|---|
arr | array | The array to flatten |
A new, flattened array.
import { flatten } from '@semantic-ui/utils';
const nestedArray = [1, [2, [3, 4]], 5];console.log(flatten(nestedArray)); // [1, 2, 3, 4, 5]
function some(collection, predicate)
Check if at least one element in the collection satisfies the predicate.
Name | Type | Description |
---|---|---|
collection | array | The collection to iterate over |
predicate | function | The function invoked per iteration |
True if any element passes the predicate check, else false.
import { some } from '@semantic-ui/utils';
const numbers = [1, 2, 3, 4, 5];const result = some(numbers, (num) => num > 3);console.log(result); // true
An alias for some
.
function sortBy(arr, key, comparator)
Sort an array of objects by a specific key.
Name | Type | Description |
---|---|---|
arr | array | The array to sort |
key | string | The key to sort by |
comparator | function | Optional custom comparison function |
A new sorted array.
import { sortBy } from '@semantic-ui/utils';
const people = [ { name: 'John', age: 30 }, { name: 'Jane', age: 25 }, { name: 'Bob', age: 35 }];const sorted = sortBy(people, 'age');console.log(sorted);// [{ name: 'Jane', age: 25 }, { name: 'John', age: 30 }, { name: 'Bob', age: 35 }]
function groupBy(array, property)
Group an array of objects by a specific property.
Name | Type | Description |
---|---|---|
array | array | The array to group |
property | string | The property to group by |
An object where keys are distinct values of the specified property, and values are arrays of elements that have that property value.
import { groupBy } from '@semantic-ui/utils';
const people = [ { name: 'John', age: 30 }, { name: 'Jane', age: 25 }, { name: 'Bob', age: 30 }];const grouped = groupBy(people, 'age');console.log(grouped);// {// '25': [{ name: 'Jane', age: 25 }],// '30': [{ name: 'John', age: 30 }, { name: 'Bob', age: 30 }]// }
function intersection(...arrays)
Find elements common to all input arrays. Automatically switches between filter and Set implementation based on total array size for optimal performance.
Name | Type | Description |
---|---|---|
arrays | …Array | Arrays to find common elements between |
Array containing elements present in all input arrays.
import { intersection } from '@semantic-ui/utils';
const arr1 = [1, 2, 3, 4];const arr2 = [3, 4, 5, 6];const arr3 = [3, 4, 6, 7];console.log(intersection(arr1, arr2, arr3)); // [3, 4]
function difference(...arrays)
Find elements unique to the first array. Automatically switches between filter and Set implementation based on total array size for optimal performance.
Name | Type | Description |
---|---|---|
arrays | …Array | First array is filtered against all others |
Array containing elements present in first array but not in any other input arrays.
import { difference } from '@semantic-ui/utils';
const arr1 = [1, 2, 3, 4];const arr2 = [3, 4, 5, 6];const arr3 = [3, 4, 6, 7];console.log(difference(arr1, arr2, arr3)); // [1, 2]
function uniqueItems(...arrays)
Find elements that appear in exactly one input array. Automatically switches between filter and Set implementation based on total array size for optimal performance.
Name | Type | Description |
---|---|---|
arrays | …Array | Arrays to compare |
Array containing elements that appear in exactly one of the input arrays.
import { uniqueItems } from '@semantic-ui/utils';
const arr1 = [1, 2, 3];const arr2 = [2, 3, 4];const arr3 = [3, 4, 5];console.log(uniqueItems(arr1, arr2, arr3)); // [1, 5]