- Array
- Constructor
- Prototype
- all(predicate)
- any(predicate)
- binarySearch(x)
- chunks(chunkSize)
- clear()
- contains(x)
- endsWith(needle)
- enumerate()
- firstItem()
- fold(initialValue, predicate)
- insert(index, element)
- isEmpty()
- isSorted()
- iter()
- lastIndex()
- lastItem()
- len()
- max()
- min()
- partition(predicate)
- product()
- reject(callbackFn)
- remove([index])
- repeat(n)
- retain(predicate)
- select(callbackFn)
- shuffle()
- splitAt(at)
- split(predicate)
- startsWith(needle)
- sum()
- truncate(len)
Creates a list of the given length
with optional filledValue
which defaults to 0
when it is not set at each position.
/** [0, 0, 0], a list of 3 elements with each set to `0` when `filledValue` is not set */
Array.filed(3);
/** [1, 1, 1], a list of 3 elements, each filled with the defined value `1` */
Array.filled(3, 1);
/** [[], [], []], a list of 3 elements, each filled with an empty array */
const a = Array.filled(3, []);
a[0][0] = 2;
a; /** [[2], [2], [2]], each filled value is pointing to the same array */
Tests if every element of the list matches a predicate
.
const a = [1, 2, 3];
a.all(x => x > 0) === true;
a.all(x => x > 5) === false;
Tests if any element of the list matches a predicate
.
const a = [1, 2, 3];
a.any(x => x > 0) === true;
a.any(x => x > 5) === false;
Binary searches this sorted array for a given element. Returns the index of the matching element if the value is found, otherwise, -1
indicates no match for x
. If there are multiple matches, then any one of the matches could be returned. Note that this function will not check if the array is always sorted. Passing an unsorted array will produce the expected result.
const s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13 , 21, 34, 55]
s.binarySearch(13) === 9;
s.binarySearch(4) === -1;
s.binarySearch(100) === -1;
s.binarySearch(1) === 2;
Returns a list of chunkSize
elements of this array.
const a = [1, 2, 3, 4, 5];
a.chunks(2); /** [[1, 2], [3, 4], [5]] */
Clears the list, removing all values.
const a = [1, 2, 3];
a.clear();
a; /** [] */
Returns true
if the array contains an element with the given value.
const a = [10, 40, 30];
a.contains(30) === true;
a.contains(50) === false;
Returns true
if needle
is a suffice of the array.
const v = [10, 40, 30];
v.endsWith(30) === true;
v.endsWith([40, 30]) == true;
v.endsWith([]) === true;
v.endsWith([50]) === false;
v.endsWith([50, 30]) === false;
const y = [];
y.endsWith([]) === true;
Iterates over the entire list which gives the current iteration count as well as the element.
const a = [1, 2, 3];
a.enumerate(); /** [[0, 1], [1, 2], [2, 3]] */
for const [idx, element] of a.enumerate() {
console.log('idx = %d, element = %d', idx, element);
}
Returns the first element of the array, or undefined
if it is empty.
const a = [10, 40, 30];
a.firstItem() === 10;
const b = [];
b.firstItem() === undefined;
Reduces a collection to a single value by iteratively combining each element of the collection with an initialValue
.
const a = [1, 2, 3];
a.fold(0, (acc, x) => acc + x) === 6;
Inserts an element at position index
within the list, shifting all elements after it to the right, where index
is between 0
and the length of the list.
const a = [1, 2, 3];
a.insert(1, 4);
a; /** [1, 4, 2, 3] */
a.insert(4, 5);
a; /** [1, 4, 2, 3, 5] */
Returns true
if the list contains no elements.
const a = [];
a.isEmpty() === true;
a.push(1);
a.isEmpty() === false;
Checks if the elements of the list are sorted.
[1, 2, 2, 9].isSorted() === true;
[1, 3, 2, 4].isSorted() === false;
[0].isSorted() === true;
[].isSorted() === true;
[0.0, 1.0, NaN].isSorted() === false;
Returns an iterator over the list.
const a = [1, 2, 4];
const iterator = a.iter();
iterator.next().value === 1;
iterator.next().value === 2;
iterator.next().value === 4;
iterator.next().done === true;
Returns the last index in the array.
[1, 2, 3].lastIndex() === 2;
[].lastIndex() === 0;
Returns the last element of the array, or undefined
if it is empty.
const v = [10, 40, 30];
v.lastItem() === 30;
const x = [];
v.lastItem() === undefined;
Returns the number of elements in the list, also referred to as its length
.
[1, 2, 3].len() === 3;
Returns the maximum element of a list.
[1, 2, 3].max() === 3;
[].max() === undefined;
Returns the minimum element of a list.
[1, 2, 3].min() === 1;
[].min() === undefined;
Creates two collections from a list.
[1, 2, 3].partition(n => n % 2 === 0); /** [[2], [1, 3]] */
Iterates over the entire list, multiplying all the elements.
[1, 2, 3].product() === 6;
Filters out the elements specified by callbackFn
. If true, it would remove the current item.
const a = [1, 2, 3, 4, 5];
a.reject(n => n > 2); // [1, 2];
Removes and returns the element at position index
within the list, shifting all elements after it to the left, where index
is between 0
and the length of the list and defaults to 0
if it is not set.
const a = [1, 2 ,3];
a.remove() === 1;
a.remove(1) === 1;
a; /** [2] */
Creates a list by repeating a slice n
times.
[1, 2].repeat(3); /** [1, 2, 1, 2, 1, 2] */
Retains only the elements specified by the predicate
. In other words, remove all elements e
such as f(e)
returns false
. This method operates in place and preserves the order of the retained elements.
const a = [1, 2, 3, 4];
a.retain(n => n % 2 === 0);
a; /** [2, 4] */
Filters in the elements specified by callbackFn
. If true, it would keep the current item.
const a = [1, 2, 3, 4, 5];
a.select(n => n > 2); // [3, 4, 5];
Shuffles the elements of the list randomly.
const a = [1, 2, 3, 4];
a.shuffle();
a; /** [2, 3, 1, 4] */
Splits the collection into two at the given index. Returns a newly allocated list. List contains elements [0, at)
, and the returned list contains elements [at, len)
.
const a = [1, 2, 3];
const b = a.splitAt(1);
a; /** [1] */
b; /** [2, 3] */
Returns a list of subslices separated by elements that match predicate
. The matched element is not contained in the subslices. If the first element is matched, an empty slice will be the first item returned. Similarly, the last item in the slice is matched, an empty slice will be the last item. If two matched elements are directly adjacent, an empty slice will be present between them.
const a = [10, 40, 33, 20];
a.split(num => num % 3 === 0); /** [[10, 40], [20]] */
const y = [10, 40, 33];
y.split(num => num % 3 === 0); /** [[10, 40], []] */
const z = [10, 6, 33, 20];
z.split(num => num % 3 === 0); /** [[10], [], 20] */
Returns true
if the needle
is a prefix of the slice.
const v = [10, 40, 30];
v.startsWith([10]) === true;
v.startsWith([10, 40]) === true;
v.startsWith([]) === true;
v.startsWith([50]) === false;
v.startsWith([10, 50]) === false;
const x = [];
x.startsWith([]) === true;
Iterates over the entire list, adding all the elements.
[1, 2, 3].sum() === 6;
Shortens the list, keeping the first len
elements and dropping the rest. If len
is greater than the list's current length, this has no effect.
const a = [1, 2, 3, 4, 5];
a.truncate(2);
a; /** [1, 2] */