Array
Index
Accessor
at
Return an item at the
indexpositionconst array = ['foo', 'bar', 'baz'];Array.at(array, 1) // Option.Some('bar')Array.at(array, -1) // Option.Some('baz') i.e. the lastArray.at(array, 99) // Option.None
size
Return the length of the array
Array.size([]) // 0Array.size(['foo', 'bar']) // 2
Constructor
empty
Always returns an empty array
Array.empty() // []
generate
Generate an array of
lengthusingmapFn(index)on each elementArray.generate(3, () => 'a');// == ['a', 'a', 'a']Array.generate(3, (index) => index * 2);// == [0, 2, 4]
of
Returns a new array from a set of items.
Array.of(1, 2, 3);// [1, 2, 3]
Predicate
every
Determines whether all the members of an array satisfy the specified test.
const isEven = (_: number) => _ % 2 === 0;Array.every([1, 2, 3], isEven); // falseArray.every([2, 4], isEven); // trueArray.every([], (value) => false); // true
includes
Determines whether an array includes a certain item, returning true or false as appropriate.
Array.includes(['a', '', 'a'], 'a'); // trueArray.includes(['a', '', 'a', '', 'a'], 'a', 1); // trueArray.includes(['a', 'b'], 'absent'); // false
isEmpty
Return true if the size of the array is 0
Array.isEmpty([]);// trueArray.isEmpty(['a', 'b', 'c']);// false
some
Determines whether the specified callback function returns true for any item of an array.
const isEven = (_: number) => _ % 2 === 0;Array.some([1, 2, 3], isEven); // trueArray.some([1, 3], isEven); // falseArray.some([], (value) => true); // false
Type
hasInstance
Alias to
Array.isArray()Array.hasInstance(Array.empty()) // trueArray.hasInstance(null)) // false
Other
concat
Concatenate all elements of arrays Equivalent to
[...array, ...extension[0], ...extension[1], ...],Array.concat([1, 2], [3, 4], [5, 6]);// [1, 2, 3, 4, 5, 6]
deleteAt
Return an array excluding the item at the
indexArray.deletedAt([1, 2, 3, 4], 1);// [1, 3, 4]
filter
Returns the items of an array that meet the condition specified in a callback function.
const array = [1, 2, 3, 4];const isEven = (_: number) => _ % 2 === 0;Array.filter(array, isEven); // [2, 4]
find
Returns the value of the first element in the array where predicate is true, and Option.None otherwise.
Array.find(['aa', 'ab', 'abc'], (value) => (value[1] === 'b'));// Option.Some('ab')Array.find(['a', 'b', 'a'], (value) => false);// Option.None
findIndex
Returns the index of the first element in the array where predicate is true, and Option.None otherwise.
Array.findIndex(['a', 'b', 'a'], (value) => (value === 'a'));// Option.Some(0)Array.findIndex(['a', 'b', 'a'], (value) => false);// Option.None
findLast
Returns the value of the last element in the array where predicate is true, and Option.None otherwise.
Array.findLast(['aa', 'ab', 'abc'], (value) => (value[1] === 'b'));// Option.Some('abc')Array.findLast(['a', 'b', 'a'], (value) => false);// Option.None
findLastIndex
Returns the index of the last element in the array where predicate is true, and Option.None otherwise.
Array.findLastIndex(['a', 'b', 'a'], (value) => (value === 'a'));// Option.Some(2)Array.findLastIndex(['a', 'b', 'a'], (value) => false);// Option.None
flatMap
Calls a defined callback function on each item of an array. Each calls should return an array. The final result is the concatenation of each arrays.
const array = ['a', 'b', 'c'];const concat = (_: string) => [_ + '_1', _ + '_2'];Array.flatMap(array, concat); // ['a_1', 'a_2', 'b_1', 'b_2', 'c_1', 'c_2']
fromAsync
Creates an array from an async iterator or iterable object.
await Array.fromAsync(function* () {let index = 0;while (index < 5) {yield Promise.resolve(index);*index += 1;}});// [0, 1, 2, 3, 4]
indexOf
Returns the index of the first occurrence of
searchItemin an array.Array.indexOf(['a', '', 'a'], 'a'); // Option.Some(0)Array.indexOf(['a', '', 'a', '', 'a'], 'a', 1); // Option.Some(2)Array.indexOf(['a', 'b'], 'absent'); // Option.Some(
insertAt
Add
itemat theindexin the arrayArray.insertAt(['a', 'b', 'c'], 1, '$');// ['a', '$', 'b', 'c']
lastIndexOf
Returns the index of the last occurrence of a specified
searchItemin an array.Array.lastIndexOf(['a', 'b', 'a'], 'a') // Option.Some(2)Array.lastIndexOf(['a', 'b', 'a', 'b', 'a'], 'a', 4); // Option.Some(2)Array.lastIndexOf(['a', 'b'], 'absent') // Option.None
map
Calls a defined callback function on each item of an array, and returns an array that contains the results.
const array = [1, 2, 3];const double = (_: number) => _ * 2;Array.map(array, double); // [2, 4, 6]
reduce
Calls the specified callback function for all the items in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
const array = ['foo', 'bar', 'baz'];const concat = (_: string, item: string) => _ + ':' + item;Array.reduce(array, concat, '$') // '$foo:bar:baz'
reduceRight
Calls the specified callback function for all the items in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
const array = ['foo', 'bar', 'baz'];const concat = (_: string, item: string) => _ + ':' + item;Array.reduceRight(array, concat, '$') // '$baz:bar:foo'
reverse
Return a reversed array
Array.reverse([1, 2, 3]);// [3, 2, 1]
setAt
Replace
itemat theindexin the arrayArray.setAt(['a', 'b', 'c'], 1, '$');// ['a', '$', 'c']
slice
Returns a section of an array.
Array.sort([1, 2, 3, 4], 1, 3);// [2, 3]
sort
Return a sorted array using
compareFnArray.sort([11, 2, 22, 1], Number.compare);// [1, 2, 11, 22]
splice
Add
itemat theindexin the arrayconst array = ['a', 'b', 'c', 'd', 'e'];// Remove 2 items starting at index 1splice(array, 1, 2); // ['a', 'd', 'e']// Insert items at index 2splice(array, 2, 0, 'x', 'y'); // ['a', 'b', 'x', 'y', 'c', 'd', 'e']// Replace items at index 1splice(array, 1, 2, 'u', 'v'); // ['a', 'u', 'v', 'd', 'e']
A collection of functions to manipulate readonly arrays.