ArrayList
No search results
Introducing: the JavaScript Array in frustration-free packaging!
What?
The ArrayList class implements a comfortable array collection. It uses a JavaScript Array as its internal storage and provides a public interface inspired by .NET’s List and LINQ Enumerable types with a few more methods of JavaScript’s Array, SQL (relational) group, join and set operations, and others. (It still returns independent arrays from each operation, so it doesn’t have the advantages of IEnumerable. There are other and larger projects for that.)
It borrows its name from the ArrayList class of .NET 1.1 which was mostly replaced by the generic (statically typed) List<T> class in .NET 2.0. This is a good fit again for the dynamically-typed nature of JavaScript.
Why?
JavaScript arrays are historically a constant source of frustration. The Array class tries to be a list, a queue and a stack all in one but does nothing right. It has improved a little over the years, yet some basic operations like removing an item are still missing. Names are also confusing, considering the Array.includes(), Set.has() and DOMTokenList.contains() methods.
The ArrayList class aims to reuse as much of JavaScript’s Array as possible but pack it into a clean and consistent API that offers all methods under the collection instance itself, instead of passing the array along as parameter or extending the Array prototype which might break in the future. The only exception is the “L” property (for “list”) that is made available to all Arrays as kind of an extension property. It allows using ArrayList methods on an Array with minimal effort and clutter.
As a result, ArrayList is compact and easy to use, increasing the developer’s productivity and happiness. The minified and gzipped code is just 3 KiB. It is also fully covered by unit tests.
How?
Here are a few examples to show its usage and the results. First some list manipulation calls:
Then methods that return a new ArrayList and don’t affect the original array or list:
Most of these methods are implemented independently from each other so they can also be copied into other applications and libraries individually. Most method implementations are also rather short. All methods have unit test cases for all possible combinations of inputs, also covering side effects.
Note:
As a general rule, all collections returned by ArrayList methods will be of the ArrayList type.
All ArrayList methods that take collections as parameters will accept Array and ArrayList instances, some might also accept any iterable.
The examples in this documentation are kept as simple as possible.
The test cases include both types of parameters where possible.
Note:
Where the Array methods expect a parameter like endIndex, referring to the last index before which to stop an operation, the ArrayList methods usually expect a count parameter, referring to the number of processed items. This is usually more intuitive, easier to write and in line with the .NET APIs.
Where?
As a modern JavaScript library, this code is compatible with the following environments:
- Firefox 62 and later (released 2018-09)
- Chrome 70 and later (released 2018-10, and browsers derived thereof)
- Safari 12 and later (released 2018-09; by spec; little tested due to the lack of Apple hardware)
- Node.js 12 and later (released 2019-04)
Earlier versions may work for a limited feature set. The vastly outdated and now abandoned Internet Explorer is explicitly incompatible with this code. Should you be stuck in the past for whatever reason, a transpiler like Babel and a few polyfills might help you out.
This library is released under the terms of the MIT licence. See the complete licence text in the code repository for details.
Documentation
This documentation lists all public members of the ArrayList class. The available parameters are listed for each method, describing which of them are optional or alternative in which combination. The description explains the behaviour and parameter requirements. The return value is shown after the arrow. A link to the Array class method is provided where there’s great similarity in their function.
A few examples further describe the usage and effect of a method. More comprehensive input and output of a method can be found in the separate test cases included in the source code.
Search
The members are categorised and can be searched (just type ahead). The search also uses aliases for many entries. Start the search text with an equals sign (=) to ignore aliases. Start the search text with the ^ sign to also only find names starting with the search term. Press Escape to reset the search.
Constructor
ArrayList
new ArrayList(array)
→ ArrayListnew ArrayList(iterable)
→ ArrayListnew ArrayList(number)
→ ArrayList
Creates a new ArrayList instance that contains the items included in the specified Array or Iterable. If an Array is specified, that Array is wrapped by the ArrayList instance and manipulations will also be seen in that Array and vice versa. If a Number is specified, the new array length is set.
Array.L
Array.L
→ ArrayList
The L property for Array instances provides access to the additional features implemented in ArrayList.
This is the only addition to other prototypes made by this library.
array.L
is a shortcut for: new ArrayList(array)
It can also be used to make use of ArrayList methods in a workflow that requires plain Arrays, together with the “array” property or toArray() method to get back the internal array.
See also: ArrayList.array, ArrayList.toArray()
Instance properties
array
array
→ Array
Gets the internal array of the ArrayList instance. This property cannot be written. The returned array is the actual object used by the ArrayList instance. It contains exactly the items of the collection. Any changes done to this array will also be reflected in the ArrayList instance.
This property is the inverse of the ArrayList constructor that takes an Array instance to wrap. It allows to switch from an Array to ArrayList (with the constructor or the “L” property added to the Array prototype) to use its manipulation methods and then switch back to Array for external use.
See also: ArrayList.toArray()
first
first
→ Any
Gets or sets the first item of the array. If there is no first item, undefined is returned.
This is an alternative to: ArrayList.array[0]
last
last
→ Any
Gets or sets the last item of the array. If the array is empty, undefined is returned and setting the property throws an error.
This is an alternative to: ArrayList.array[ArrayList.length - 1]
or ArrayList.get(-1)
length
length
→ Number
Gets the number of items in the array. This property cannot be written.
This returns the same value as: ArrayList.array.length
See also: Array.length
second
second
→ Any
Gets or sets the second item of the array. If there is no second item, undefined is returned.
This is an alternative to: ArrayList.array[1]
Item retrieval methods
These instance methods retrieve a single item from the array or a single aggregated value computed from the array.
aggregate
aggregate(reducer)
→ Anyaggregate(initialValue, reducer)
→ Any
Applies an accumulator function over the array. The reducer function is called with two arguments and returns a single value that combines both. Then the function is called with the previous return value and the next array item, until the last item. If the initialValue is unset and the array is empty, undefined is returned. If the initialValue is set, it is the first argument of the first call of the reducer function.
See also: Array.reduce(), ArrayList.aggregateReversed()
aggregateReversed
aggregateReversed(reducer)
→ AnyaggregateReversed(initialValue, reducer)
→ Any
Applies an accumulator function over the array in reversed order. See the aggregate() method for more details.
See also: Array.reduceRight(), ArrayList.aggregate()
all
all(predicate)
→ Booleanall(predicate, thisArg)
→ Boolean
Determines whether all items in the array satisfy a condition. The predicate function gets three arguments:
- The array item
- Its index in the array
- The entire array (the internal Array instance)
If thisArg is set, it is assigned to this
when executing the predicate function.
If the array is empty, true is returned.
The parameter semantics is the same as the Array.every() method.
See also: Array.every()
any
any()
→ Booleanany(predicate)
→ Booleanany(predicate, thisArg)
→ Boolean
Determines whether any item in the array satisfies a condition. The predicate function gets three arguments:
- The array item
- Its index in the array
- The entire array (the internal Array instance)
If no predicate is specified, the return value is true only if the array contains any items.
If thisArg is set, it is assigned to this
when executing the predicate function.
If the array is empty, false is returned.
The parameter semantics is the same as the Array.some() method.
See also: Array.some()
average
average()
→ Numberaverage(selector)
→ Number
Computes the average value of items in the array. The optional selector function is called for each item of the array and returns the value to use for the average computation. If the selector is omitted, the array must contain numbers only. If the array is empty, undefined is returned.
binarySearch
binarySearch(item)
→ NumberbinarySearch(item, comparer)
→ Number
Searches the sorted array for an item using the comparer and returns the zero-based index of the item.
If the item is not found, the return value is a negative number that is the bitwise complement (~
) of the index of the next item that is larger than item or, if there is no larger item, the bitwise complement of the length property.
contains
contains(item)
→ Booleancontains(item, startIndex)
→ Boolean
Determines whether an item exists in the array. The parameter semantics is the same as the Array.includes() method.
See also: Array.includes()
count
count()
→ Numbercount(predicate)
→ Number
Counts the items in the array that satisfy a condition. If no predicate is specified, the array length is returned.
equals
equals(other)
→ Boolean
Determines whether two arrays contain the same items in the same order. The values are compared with the === operator.
See also: ArrayList.setEquals()
find
find(predicate)
→ Anyfind(predicate, thisArg)
→ Any
Returns the first item in the array that satisfies a condition. If no values satisfy the condition, undefined is returned. The predicate function gets three arguments:
- The array item
- Its index in the array
- The entire array (the internal Array instance)
If thisArg is set, it is assigned to this
when executing the predicate function.
The parameter semantics is the same as the Array.find() method.
See also: Array.find()
findIndex
findIndex(predicate)
→ NumberfindIndex(predicate, thisArg)
→ Number
Returns the index of the first item in the array that satisfies a condition. If no values satisfy the condition, the return value is -1. The predicate function gets three arguments:
- The array item
- Its index in the array
- The entire array (the internal Array instance)
If thisArg is set, it is assigned to this
when executing the predicate function.
The parameter semantics is the same as the Array.findIndex() method.
See also: Array.findIndex(), ArrayList.find()
findLast
findLast(predicate)
→ AnyfindLast(predicate, thisArg)
→ Any
Returns the last item in the array that satisfies a condition. If no values satisfy the condition, undefined is returned. The predicate function gets three arguments:
- The array item
- Its index in the array
- The entire array (the internal Array instance)
If thisArg is set, it is assigned to this
when executing the predicate function.
The parameter semantics is the same as the Array.find() method.
findLastIndex
findLastIndex(predicate)
→ NumberfindLastIndex(predicate, thisArg)
→ Number
Returns the index of the last item in the array that satisfies a condition. If no values satisfy the condition, the return value is -1. The predicate function gets three arguments:
- The array item
- Its index in the array
- The entire array (the internal Array instance)
If thisArg is set, it is assigned to this
when executing the predicate function.
The parameter semantics is the same as the Array.findIndex() method.
See also: ArrayList.findLast()
get
get(index)
→ Any
Returns the item at the positive or negative index of the array. If the index is negative, it is counted from the end, i.e. -1 is the last, -2 the second-last item, etc. If the index is outside the range of the array, undefined is returned.
See also: Array[index], ArrayList.set()
indexOf
indexOf(item)
→ Number
Returns the first index of the item in the array. If the item does not exist in the array, the return value is -1.
See also: Array.indexOf()
join
join()
→ Stringjoin(separator)
→ String
Returns a string by concatenating all of the elements of the array, separated by commas or the specified separator string. If the array has only one item, then that item will be returned without using the separator. If the array is empty, an empty string is returned.
See also: Array.join()
lastIndexOf
lastIndexOf(item)
→ Number
Returns the last index of the item in the array. If the item does not exist, the return value is -1.
See also: Array.lastIndexOf()
max
max()
→ Numbermax(selector)
→ Number
Computes the maximum value of the items in the array. The optional selector function is called for each item of the array and returns the value to use for the maximum computation. If the selector is omitted, the array must contain numbers only. The selector must return numbers only. If the array is empty, undefined is returned.
maxBy
maxBy(selector)
→ Any
Returns the item in the array with the maximum value returned by the selector. The selector function is called for each item of the array and returns the value to use for the maximum test. If the array is empty, undefined is returned.
Added in v2.1.0
min
min()
→ Numbermin(selector)
→ Number
Computes the minimum value of the items in the array. The optional selector function is called for each item of the array and returns the value to use for the minimum computation. If the selector is omitted, the array must contain numbers only. The selector must return numbers only. If the array is empty, undefined is returned.
minBy
minBy(selector)
→ Any
Returns the item in the array with the minimum value returned by the selector. The selector function is called for each item of the array and returns the value to use for the minimum test. If the array is empty, undefined is returned.
Added in v2.1.0
sum
sum()
→ Numbersum(selector)
→ Number
Computes the sum of the items in the array. The optional selector function is called for each item of the array and returns the value to use for the sum computation. If the selector is omitted, the array must contain numbers only. If the array is empty, undefined is returned.
Manipulation methods
These instance methods alter the contents of the array. Most of these methods return the instance for chained calls, except the remove and replace methods.
add
add(item)
→ this
Adds an item to the end of the array.
See also: Array.push()
addRange
addRange(items)
→ this
Adds many items to the end of the array.
See also: ArrayList.concat() (copying alternative)
clear
clear()
→ this
Removes all items from the array. This is equivalent of setting array.length = 0.
fill
fill(value)
→ thisfill(value, start)
→ thisfill(value, start, count)
→ this
Changes all elements in the array to a static value. The length of the array is not changed. If the array is empty, nothing is changed. If the start is negative, it is counted from the end, i.e. -1 is the last, -2 the second-last item, etc. If count is omitted, all items from the start index to the end of the array are changed. If count is negative, the items until (excluding) the count index from the end of the array are changed, i.e. -1 excludes the last, -2 excludes the 2 last items, etc.
See also: Array.fill()
insert
insert(index, item)
→ this
Inserts an item into the array at the specified index. If the index is negative, it is counted from the end, i.e. -1 is the last, -2 the second-last item, etc. If index is greater than the array length, the item is added at the end of the array. If the negative index points to a position before the start of the array, the item is inserted at the beginning of the array.
insertRange
insertRange(index, items)
→ this
Inserts many items into the array at the specified index. If the index is negative, it is counted from the end, i.e. -1 is the last, -2 the second-last item, etc. If index is greater than the array length, the items are added at the end of the array. If the negative index points to a position before the start of the array, the item is inserted at the beginning of the array.
remove
remove(item)
→ Boolean
Removes an item by value from the array. The return value is true if the item was found; otherwise, false.
removeAll
removeAll(predicate)
→ ArrayListremoveAll(predicate, thisArg)
→ ArrayList
Removes and returns all items from the array that satisfy a condition. If no items were removed, the return valus is an empty array. The predicate function gets three arguments:
- The array item
- Its index in the array
- The entire array (the internal Array instance)
If thisArg is set, it is assigned to this
when executing the predicate function.
removeAt
removeAt(index)
→ Any
Removes and returns an item by index from the array. If the index is negative, it is counted from the end, i.e. -1 is the last, -2 the second-last item, etc. If index is greater than the array length, nothing is removed and undefined is returned. If the negative index points to a position before the start of the array, the first item is removed and returned.
removeFirst
removeFirst()
→ Any
Removes and returns the first item from the array. If the array is empty, undefined is returned.
See also: Array.shift(), ArrayList.skip() (copying alternative)
removeLast
removeLast()
→ Any
Removes and returns the last item from the array. If the array is empty, undefined is returned.
See also: Array.pop(), ArrayList.skipLast() (copying alternative)
removeRange
removeRange(items)
→ ArrayListremoveRange(start)
→ ArrayListremoveRange(start, count)
→ ArrayList
Removes and returns many items from the array, either by index and count or the specified items. If the start is negative, it is counted from the end, i.e. -1 is the last, -2 the second-last item, etc. If count is omitted, all items from the start index to the end of the array are removed. If count is negative, the items until (excluding) the count index from the end of the array are removed, i.e. -1 keeps the last, -2 keeps the 2 last items, etc. If no items were removed, the return valus is an empty array.
replace
replace(oldItem, newItem)
→ Boolean
Replaces the first item by the value of oldItem in the array. The return value is true if the old item was found; otherwise, false.
reverse
reverse()
→ this
Reverses the array in-place.
See also: Array.reverse(), ArrayList.reversed() (copying alternative)
set
set(index, value)
→ this
Sets the item at the positive or negative index of the array. If the index is negative, it is counted from the end, i.e. -1 is the last, -2 the second-last item, etc. If index is greater than the array length, empty slots will be created between the old last and the new item. If the negative index points to a position before the start of the array, the property representing the negative number is created but the set value is not part of the iterable items of the array. This behaviour is identical to setting arbitrary indexes of a plain Array.
See also: Array[index], ArrayList.get()
shuffle
shuffle()
→ this
Shuffles the array using a Fisher-Yates algorithm.
sort
sort()
→ thissort(comparer)
→ this
Sorts the array using string order or the comparer function.
See also: Array.sort()
sortBy
sortBy(keySelector, …)
→ this
Sorts the array in ascending order according to a key. Secondary sorting is done by providing more key selector arguments. An argument of 1 or -1 sets the sort direction of the following keys ascending or descending, respectively.
The comparison is done with the <
(less than) and >
(greater than) operators.
Two numbers are compared by their numeric value, two strings are compared lexically.
null
s always follow after values, undefined
s follow after null
s.
Mixed-type comparisons have unpredictable results.
See also: ArrayList.orderBy() (copying alternative)
sortByDescending
sortByDescending(keySelector, …)
→ this
Sorts the array in descending order according to a key. Secondary sorting is done by providing more key selector arguments. An argument of 1 or -1 sets the sort direction of the following keys ascending or descending, respectively.
The comparison is done with the <
(less than) and >
(greater than) operators.
Two numbers are compared by their numeric value, two strings are compared lexically.
null
s always follow after values, undefined
s follow after null
s.
Mixed-type comparisons have unpredictable results.
See the sortBy() method for an example. This method differs in that the initial sort order is descending instead of ascending. Both methods behave identically if the first argument is 1 or -1.
See also: ArrayList.orderByDescending() (copying alternative)
sortByUseLocale
sortByUseLocale(keySelector, …)
→ this
Sorts the array in ascending order according to a key, using locale string comparison. Secondary sorting is done by providing more key selector arguments. An argument of 1 or -1 sets the sort direction of the following keys ascending or descending, respectively.
The comparison is done with the localeCompare()
method if both values are strings; otherwise, <
(less than) and >
(greater than) operators.
Two numbers are compared by their numeric value, two strings are compared according to the current locale.
null
s always follow after values, undefined
s follow after null
s.
Mixed-type comparisons have unpredictable results.
See the sortBy() method for an example. This method differs in the string comparison.
See also: ArrayList.orderByUseLocale() (copying alternative)
Added in v2.1.0
sortByDescendingUseLocale
sortByDescendingUseLocale(keySelector, …)
→ this
Sorts the array in descending order according to a key, using locale string comparison. Secondary sorting is done by providing more key selector arguments. An argument of 1 or -1 sets the sort direction of the following keys ascending or descending, respectively.
The comparison is done with the localeCompare()
method if both values are strings; otherwise, <
(less than) and >
(greater than) operators.
Two numbers are compared by their numeric value, two strings are compared according to the current locale.
null
s always follow after values, undefined
s follow after null
s.
Mixed-type comparisons have unpredictable results.
See the sortBy() method for an example. This method differs in the string comparison and that the initial sort order is descending instead of ascending.
See also: ArrayList.orderByDescendingUseLocale() (copying alternative)
Added in v2.1.0
sortNumeric
sortNumeric()
→ this
Sorts the array using numerical order. The array must contain numbers only.
Copy methods
These instance methods return multiple items from the array in a new collection and do not alter the contents of the instance.
Note:
Most of these methods return an ArrayList instance even though the example output shown looks like a plain Array for brevity.
After all, JSON serialisation of ArrayList and Array have the same result.
Hint:
Copying the result items to a new array instance allocates additional memory.
If the method is called on an ArrayList instance that is already a new collection, for example in multiple chained method calls, a manipulating alternative (where available) may in some cases provide better runtime or memory performance.
chunk
chunk(length)
→ ArrayListchunk(length, pad)
→ ArrayList
Chunks the array into multiple arrays of a maximum length, with optional padding of the last shorter chunk. Each subarray is again an ArrayList instance. The result can be converted to a plain Array of Arrays with the toArray() method.
concat
concat()
→ ArrayListconcat(array, …)
→ ArrayList
Concatenates the array with the other specified arrays. If no parameter is specified, this method behaves the same as the clone() method.
See also: Array.concat(), ArrayList.addRange() (manipulating alternative)
clone
clone()
→ ArrayList
Returns a new array with the items from the array.
See also: Array.concat()
cycle
cycle(count)
→ ArrayList
Returns a new array containing the items from the array, cycling until the target length has been reached. If the array is empty, an empty array is returned.
flat
flat()
→ ArrayListflat(depth)
→ ArrayList
Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth. If the depth is not set, it defaults to 1.
See also: Array.flat()
getRange
getRange(start)
→ ArrayListgetRange(start, count)
→ ArrayList
Returns items from the array. If the start is negative, it is counted from the end, i.e. -1 is the last, -2 the second-last item, etc. If count is omitted, all items from the start index to the end of the array are returned. If count is negative, the items until (excluding) the count index from the end of the array are returned, i.e. -1 excludes the last, -2 excludes the 2 last items, etc.
See also: Array.slice()
groupBy
groupBy(keySelector)
→ MapgroupBy(keySelector, elementSelector)
→ Map
Groups the items of the array according to the key selector function and returns a new Map from each group key and an ArrayList of the items in the group. The order of the items in each returned group is the same as in the original array.
innerJoin
innerJoin(other)
→ ArrayListinnerJoin(other, constraint)
→ ArrayListinnerJoin(other, constraint, selector)
→ ArrayList
Correlates the elements of two arrays based on a join constraint. The constraint function is called with two arguments, one item from each array, and returns true if the combination matches. The result contains items that match in both arrays. This corresponds to an inner join in SQL. The order of the returned items is the same as the original array. This method has a runtime complexity of O(n²).
If no constraint function is specified, the result is a cross join, or cartesian product, of the length of this array multiplied by the length of the other array. If no selector is specified, Object.assign() is used to combine the properties of both matching items, where equally named properties of the other array overwrite properties of this array. To keep separate properties with the same name from both array items, a selector must be used to project the properties to different names.
leftJoin
leftJoin(other)
→ ArrayListleftJoin(other, constraint)
→ ArrayListleftJoin(other, constraint, selector)
→ ArrayList
Correlates the elements of two arrays based on a join constraint. The constraint function is called with two arguments, one item from each array, and returns true if the combination matches. The result contains all items from this array, and the matched items from the other array. This corresponds to a left outer join in SQL. The order of the returned items is the same as the original array. This method has a runtime complexity of O(n²).
If no constraint function is specified, the result is a cross join, or cartesian product, of the length of this array multiplied by the length of the other array. If no selector is specified, Object.assign() is used to combine the properties of both matching items, where equally named properties of the other array overwrite properties of this array. To keep separate properties with the same name from both array items, a selector must be used to project the properties to different names. If there was no match in the other array, the second argument to the selector function is null.
orderBy
orderBy(keySelector, …)
→ ArrayList
Returns a copy of the array in ascending order according to a key. Secondary sorting is done by providing more key selector arguments. An argument of 1 or -1 sets the sort direction of the following keys ascending or descending, respectively.
The comparison is done with the <
(less than) and >
(greater than) operators.
Two numbers are compared by their numeric value, two strings are compared lexically.
null
s always follow after values, undefined
s follow after null
s.
Mixed-type comparisons have unpredictable results.
See also: ArrayList.sortBy() (manipulating alternative)
orderByDescending
orderByDescending(keySelector, …)
→ ArrayList
Returns a copy of the array in descending order according to a key. Secondary sorting is done by providing more key selector arguments. An argument of 1 or -1 sets the sort direction of the following keys ascending or descending, respectively.
The comparison is done with the <
(less than) and >
(greater than) operators.
Two numbers are compared by their numeric value, two strings are compared lexically.
null
s always follow after values, undefined
s follow after null
s.
Mixed-type comparisons have unpredictable results.
See the orderBy() method for an example. This method differs in that the initial sort order is descending instead of ascending. Both methods behave identically if the first argument is 1 or -1.
See also: ArrayList.sortByDescending() (manipulating alternative)
orderByUseLocale
orderByUseLocale(keySelector, …)
→ ArrayList
Returns a copy of the array in ascending order according to a key, using locale string comparison. Secondary sorting is done by providing more key selector arguments. An argument of 1 or -1 sets the sort direction of the following keys ascending or descending, respectively.
The comparison is done with the localeCompare()
method if both values are strings; otherwise, <
(less than) and >
(greater than) operators.
Two numbers are compared by their numeric value, two strings are compared according to the current locale.
null
s always follow after values, undefined
s follow after null
s.
Mixed-type comparisons have unpredictable results.
See the orderBy() method for an example. This method differs in the string comparison.
See also: ArrayList.sortByUseLocale() (manipulating alternative)
Added in v2.1.0
orderByDescendingUseLocale
orderByDescendingUseLocale(keySelector, …)
→ ArrayList
Returns a copy of the array in descending order according to a key, using locale string comparison. Secondary sorting is done by providing more key selector arguments. An argument of 1 or -1 sets the sort direction of the following keys ascending or descending, respectively.
The comparison is done with the localeCompare()
method if both values are strings; otherwise, <
(less than) and >
(greater than) operators.
Two numbers are compared by their numeric value, two strings are compared according to the current locale.
null
s always follow after values, undefined
s follow after null
s.
Mixed-type comparisons have unpredictable results.
See the orderBy() method for an example. This method differs in the string comparison and that the initial sort order is descending instead of ascending.
See also: ArrayList.sortByDescendingUseLocale() (manipulating alternative)
Added in v2.1.0
outerJoin
outerJoin(other)
→ ArrayListouterJoin(other, constraint)
→ ArrayListouterJoin(other, constraint, selector)
→ ArrayList
Correlates the elements of two arrays based on a join constraint. The constraint function is called with two arguments, one item from each array, and returns true if the combination matches. The result contains all items when there is a match in either this or the other array. This corresponds to a full outer join in SQL. The order of the returned items is the same as the original array, followed by the new items from the other array. This method has a runtime complexity of O(2n²).
If no constraint function is specified, the result is a cross join, or cartesian product, of the length of this array multiplied by the length of the other array. If no selector is specified, Object.assign() is used to combine the properties of both matching items, where equally named properties of the other array overwrite properties of this array. To keep separate properties with the same name from both array items, a selector must be used to project the properties to different names. If there was no match in this or the other array, one of the arguments to the selector function is null.
rearrange
rearrange(mapper)
→ ArrayList
Returns a copy of the array with the items rearranged by a mapping function that maps the source index to the target index. The items are rearranged in their order in the array. If the mapping function returns a target index twice, the last assigned value will overwrite previous values at that index.
repeat
repeat(count)
→ ArrayList
Returns a copy of the array repeated multiple times. If count is 0, the returned array is empty.
reversed
reversed()
→ ArrayList
Returns a reversed copy of the array.
See also: ArrayList.reverse() (manipulating alternative)
select
select(selector)
→ ArrayListselect(selector, thisArg)
→ ArrayList
Projects each item in the array into a new form by calling the selector function for each item.
The returned array contains as many items as the array, with each return value of the selector function called with the original item as parameter.
If thisArg is set, it is assigned to this
when executing the selector function.
The parameter semantics is the same as the Array.map() method.
See also: Array.map()
selectManyRecursive
selectManyRecursive(childrenSelector)
→ ArrayList
Recursively collects all items from the array and their children into a flat array. The returned array contains as many items as the array and all child items that can be found with the selector function. The childrenSelector function is called for each item with the item as parameter. It should return an array of all children of the item (can be empty), or a falsy value if there are no children.
skip
skip(count)
→ ArrayList
Returns a copy of the array without the first items.
See also: Array.slice(), ArrayList.removeFirst() (manipulating alternative)
skipLast
skipLast(count)
→ ArrayList
Returns a copy of the array without the last items.
See also: Array.slice(), ArrayList.removeLast() (manipulating alternative)
skipWhile
skipWhile(predicate)
→ ArrayList
Returns a copy of the array without the first items that satisfy a condition. As soon as an item does not satisfy the condition, that and all subsequent items until the end of the array are returned, satisfying the condition or not.
take
take(count)
→ ArrayList
Returns the first items of the array.
See also: Array.slice()
takeAfter
takeAfter(item)
→ ArrayList
Returns all items in the array that occur after the specified item, not including the item itself.
takeBefore
takeBefore(item)
→ ArrayList
Returns all items in the array that occur before the specified item, not including the item itself.
takeEvery
takeEvery(step)
→ ArrayListtakeEvery(step, start)
→ ArrayListtakeEvery(step, start, count)
→ ArrayList
Returns every nth item of the array. If start is omitted, the first returned item is the first item of the array. The count defines how many items will be returned (if available), not how many items from the array will be considered.
takeLast
takeLast(count)
→ ArrayList
Returns the last items of the array.
See also: Array.slice()
takeWhile
takeWhile(predicate)
→ ArrayList
Returns the first items of the array that satisfy a condition. As soon as an item does not satisfy the condition, no further items of the array are returned, satisfying the condition or not.
toArray
toArray()
→ Array
Returns a plain Array of the items of the array, also converting all nested ArrayLists into Arrays. The returned array is a new instance that contains the items from the internal array. Any changes done to this array will not affect the ArrayList instance.
See also: ArrayList.array
toMap
toMap()
→ MaptoMap(keySelector)
→ MaptoMap(keyName)
→ MaptoMap(keySelector, valueSelector)
→ MaptoMap(keyName, valueSelector)
→ MaptoMap(keySelector, valueName)
→ MaptoMap(keyName, valueName)
→ Map
Returns a Map of the items of the array. The map key is determined by calling the keySelector function. The corresponding value is determined by calling the valueSelector function. The items are copied in their order in the array. If the key function returns a key twice, the last assigned value will overwrite previous values for that key.
If no keySelector is provided, the source array must contain arrays with exactly 2 items each, as for the Map constructor. The first of each array items is then used as key and the second item is the corresponding value.
If no valueSelector is provided, each source array item is used as the value.
Both the keySelector and valueSelector functions get three arguments:
- The array item
- Its index in the array
- The entire array (the internal Array instance)
If keySelector or valueSelector is a string instead of a function, it specifies the name of the property to read from each item object.
See the toObject() method for examples.
See also: ArrayList.toObject()
toObject
toObject()
→ ObjecttoObject(keySelector)
→ ObjecttoObject(keyName)
→ ObjecttoObject(keySelector, valueSelector)
→ ObjecttoObject(keyName, valueSelector)
→ ObjecttoObject(keySelector, valueName)
→ ObjecttoObject(keyName, valueName)
→ Object
Returns an Object of the items of the array. The object key is determined by calling the keySelector function which must return a string value suitable for a JavaScript object property name. The corresponding value is determined by calling the valueSelector function. The items are copied in their order in the array. If the key function returns a key twice, the last assigned value will overwrite previous values for that key.
If no keySelector is provided, the source array must contain arrays with exactly 2 items each, as for Object.fromEntries(). The first of each array items is then used as key and the second item is the corresponding value.
If no valueSelector is provided, each source array item is used as the value.
Both the keySelector and valueSelector functions get three arguments:
- The array item
- Its index in the array
- The entire array (the internal Array instance)
If keySelector or valueSelector is a string instead of a function, it specifies the name of the property to read from each item object.
The inverse operation of the last example would be this:
See also: ArrayList.toMap()
transpose
transpose()
→ ArrayListtranspose(chunkSize)
→ ArrayList
Returns an array of arrays where each array contains the first/second/etc. values from each of the current arrays. It transposes a data table or matrix made of arrays of arrays of the values, also called zip/unzip. Each subarray is again an ArrayList instance. The result can be converted to a plain Array of Arrays with the toArray() method.
out ↓ | |||
in → | a1 | a2 | a3 |
b1 | b2 | b3 |
A single array can be converted for this method with the chunk() method. The result can be converted to a single array with the flat() method.
If the instance only contains a flat array, i.e. not an array of arrays, the method transposes it as a flat array directly. Only in this case, the chunkSize is required. The returned array will then also be flat.
where
where(predicate)
→ ArrayListwhere(predicate, thisArg)
→ ArrayList
Returns all items from the array that satisfy a condition. Items that do not satisfy the condition are skipped in the returned array. The order of the returned items is the same as the original array. The predicate function gets three arguments:
- The array item
- Its index in the array
- The entire array (the internal Array instance)
If thisArg is set, it is assigned to this
when executing the predicate function.
The parameter semantics is the same as the Array.filter() method.
See also: Array.filter()
Set methods
These instance methods provide set operations on the array. Most of these methods should be available in the built-in Set type, but unfortunately are not.
distinct
distinct()
→ ArrayList
Returns a new array with the items from the array, without duplicates. The order of the returned items is the same as the original array.
See also: new Set()
except
except(other)
→ ArrayList
Returns a new array with the unique items from the array, without (“minus”) all items from the other array, also called set difference. The order of the returned items is the same as the original array.
intersect
intersect(other)
→ ArrayList
Returns a new array with the unique items that exist in both the array and the other array. This corresponds to the boolean operator AND. The order of the returned items is the same as the original array.
isSubsetOf
isSubsetOf(other)
→ Boolean
Determines whether the array is a subset of the other array. If both arrays contain the same distinct items, the return value is true.
isSupersetOf
isSupersetOf(other)
→ Boolean
Determines whether the array is a superset of the other array. If both arrays contain the same distinct items, the return value is true.
setEquals
setEquals(other)
→ Boolean
Determines whether two arrays contain the same unique items but not necessarily in the same order (or quantity). Items are compared with Set.has().
See also: ArrayList.equals()
symmetricDifference
symmetricDifference(other)
→ ArrayList
Returns a new array with the unique items that exist in either the array or the other array but not in both (called symmetric difference). This corresponds to the boolean operator XOR (exclusive OR). The order of the returned items is the same as the original array, followed by the new items from the other array.
union
union(other)
→ ArrayList
Returns a new array with the unique items from the array and also from the other array. This corresponds to the boolean operator OR. The order of the returned items is the same as the original array, followed by the new items from the other array.
Void methods
These instance methods do not return items from the array and do not alter the contents of the instance.
forEach
forEach(action)
→ thisforEach(action, thisArg)
→ this
Calls a function once for each item in the array. The callback function gets three arguments:
- The array item
- Its index in the array
- The entire array (the internal Array instance)
If thisArg is set, it is assigned to this
when executing the callback function.
The parameter semantics is the same as the Array.forEach() method.
See also: Array.forEach()
Infrastructure methods
toJSON
toJSON()
→ Array
Returns the internal Array so that an ArrayList instance can be serialised to JSON just like a regular Array. Be aware that the deserialised object will be an Array, not an ArrayList, because JSON cannot maintain the object type.
See also: JSON.stringify()
toString
toString()
→ String
Returns the result of the toString() method on the internal Array.
See also: Array.toString()
Symbol.iterator
[Symbol.iterator]
→ Function
Returns the iterator function of the ArrayList instance.
Static methods
version
ArrayList.version
→ String
Returns the version identifier string of the ArrayList library.
range
ArrayList.range(start, count)
→ ArrayListArrayList.range(start, count, step)
→ ArrayList
Creates an array that contains integer numbers from the start value, with the specified step or 1. The step can be any integer (positive or negative) other than 0.