Default implementation of IList<T>.
Implements
- IIList<T>
- I
- IICollection<T>
- IIEnumerable<T>
Remarks
This implementation provides fast random indexed access. However, removing or adding elements from the beginning or in the middle of the list will be slower.
Type Parameters
T
- The type of the list's elements.
Members
Show:
Constructors
Creates a new instance and fills it with the elements of the given enumerable.
Creates a new instance and fills it with the elements of the given
enumerable.Parameters
- enumerable: IEnumerable<T>
- The enumerable to fill the list with.
- mapFunction?: function(T, number, IEnumerable<T>): T
- The optional function to call for each element to map the generate the elements in the newly created list. The first argument is the element, the second one the index and the third one is the
enumerableitself (after possible conversion).
Properties
readonly
Implements
ICollection.isReadOnlyreadonlyfinal
Implements
ICollection.sizeMethods
Adds the elements of the given collection to this list.
Adds the elements of the given collection to this list.
Creates a wrapped enumerable that has one or several elements appended to it.
Creates a wrapped enumerable that has one or several elements appended to it.
In contrast to concat, this method will not treat the
items any special if they are array-like or enumerable-like.Parameters
- items: T
- The item(s) to prepend to the enumeration
Return Value
- IEnumerable<T>
- A new live view over the original enumerable that has each of the
itemsappended to it.
Examples
const list = List.fromArray([1, 2, 3, 4])
const appendedEnumerable = list.append(5, 6)
console.log(appendedEnumerable.toArray()) // yields [1,2,3,4,5,6]
// show live updates
list.add(4.5)
console.log(appendedEnumerable.toArray()) // yields [1,2,3,4,4.5,5,6]See Also
Implements
IEnumerable.appendDefined in
IEnumerable.appendThis is modeled after the
Array.prototype.at function, with the important difference, that null will be returned instead of undefined in the case of an invalid index.Parameters
- index: number
- The index of the element to return. Supports relative indexing from the end of the enumerable when passed a negative index. In other words, if a negative number is used, the returned element will be found by counting back from the end of the enumerable.
Return Value
- T
- The element at the given
index, ornullif the index is not within the valid range of this enumerable.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 4])
console.log(enumerable.at(0)) // yields 1
console.log(enumerable.at(-1)) // yields 4
console.log(enumerable.at(5)) // yields null!
// using ?? for default values
console.log(enumerable.at(10) ?? 0) // yields 0
// use ! when you know that this is valid
console.log(enumerable.at(0)!.toFixed())
// use optional chaining when you are unsure whether the index is valid
console.log(enumerable.at(20)?.toFixed())
// combine optional chaining and null-coalescing operator when you are unsure
console.log(enumerable.at(30)?.toLocaleString() ?? 'no value')Implements
IEnumerable.atDefined in
IEnumerable.atfinal
Implements
ICollection.clearConcatenates this enumerable with the given other elements in a new dynamic IEnumerable<T>.
Concatenates this enumerable with the given other
elements in a new dynamic IEnumerable<T>.Parameters
- elements: any
- Rest arguments to be appended. In case the elements are enumerable-like, too, they will be will be enumerated after the others have been enumerated. But this can also be simple objects of type
Tin which case they will be appended to the enumerable just like with append
Return Value
- IEnumerable<T>
- A dynamic composite enumerable.
Examples
const enumerable1 = IEnumerable.from([1, 2])
const enumerable2 = IEnumerable.from([3, 4])
console.log(enumerable1.concat(enumerable2).toArray()) // yields [1,2,3,4]
// single values can be used, too
console.log(enumerable1.concat(3, 4).toArray()) // yields [1,2,3,4]
// arrays are flattened
console.log(enumerable1.concat([5, 6], [7, 8]).toArray()) // now contains [1,2,5,6,7,8]
// works with arrays and array-like collections, too
console.log(enumerable1.concat([3, 4]).toArray()) // yields [1,2,3,4]const list1 = List.fromArray([1, 2])
const list2 = List.fromArray([3, 4])
const result = list1.concat(list2)
console.log(result.toArray()) // yields [1,2,3,4]
list1.add(2.5)
// result changed also
console.log(result.toArray()) // yields [1,2,2.5,3,4]
list2.add(5)
// result changed also
console.log(result.toArray()) // yields [1,2,2.5,3,4,5]See Also
API
- append
Implements
IEnumerable.concatDefined in
IEnumerable.concatfinal
Parameters
- array: T[]
- The array to copy the elements to.
- arrayIndex: number
- The index in the given array where the first element should be copied to.
Implements
ICollection.copyToShallow copies part of this list to another location in the same list and returns it.
Shallow copies part of this list to another location in the same list and returns it.
This function mimics the JavaScript
Array.copyWithin() function, but returns a List<T> instead of an array.final
Parameters
- target: number
- Index at which to copy the sequence to. If negative
targetwill be counted from the end. - begin?: number
- Index at which to start copying from,
0if omitted. If negativebeginwill be counted from the end. - end?: number
- The index of the element after the last element at which to end copying elements from. Defaults to the length of this list. If negative
endwill be counted from the end.
Return Value
- List<T>
- A new, shallow, independent copy of this list.
Creates a wrapped enumerable that yields each element only once.
Creates a wrapped enumerable that yields each element only once.
To determine equality between objects that implement a yFiles type,
equals and hashCode methods are used if present.Parameters
- keySelector?: function(T): any
- The optional function that determines the key to determine distinctness of the elements. If omitted, the element itself is used.
Return Value
- IEnumerable<T>
- A new live view over the original enumerable that yields each element with a given key at most once.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 3, 4])
const result = enumerable.distinct()
console.log(result.toArray()) // yields [1,2,3,4]See Also
API
- HashMap
Implements
IEnumerable.distinctDefined in
IEnumerable.distinctCreates a wrapped view of an enumerable which drops the given amount of elements.
Creates a wrapped view of an enumerable which drops the given amount of elements.
Parameters
- amount: number
- The number of elements to drop from the beginning. If negative, the elements will be skipped from the end of the enumeration.
Return Value
- IEnumerable<T>
- A new live view over the original enumerable that drops
amountelements.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 4, 5, 6, 7])
const result = enumerable.drop(3)
console.log(result.toArray()) // yields [4,5,6,7]See Also
API
- dropWhile
Implements
IEnumerable.dropDefined in
IEnumerable.dropCreates a wrapped view of an enumerable which skips a number of elements at the beginning while the given predicate yields true.
Creates a wrapped view of an enumerable which skips a number of elements at the beginning while the given predicate yields
true.While the
predicate yields true for the elements of the original enumerable, the elements are skipped. The new enumeration will start with the first element for which the predicate yields false. All subsequent elements will be yielded.Parameters
- predicate: function(T, number, IEnumerable<T>): boolean
- The predicate function that determines what elements should be skipped. While the predicate yields
truefor the provided item, the element will not be yielded.
Return Value
- IEnumerable<T>
- A new live view over the original enumerable that discards elements at the beginnging of the original enumeration while the predicate yields
true.
Examples
const enumerable = IEnumerable.from([
'a',
'b',
'c',
'd',
'a',
'b',
'c',
'd',
])
const result = enumerable.dropWhile(
(element) => element.charCodeAt(0) < 'c'.charCodeAt(0),
)
console.log(result.toArray()) // yields ["c", "d", "a", "b", "c", "d"]See Also
Implements
IEnumerable.dropWhileDefined in
IEnumerable.dropWhileDetermines whether every element of the enumerable matches the given predicate.
Determines whether every element of the enumerable matches the given
predicate.Parameters
- predicate: function(T, number, IEnumerable<T>): boolean
- A predicate function which returns
trueif the element matches a condition. - thisArg?: any
- The optional object to use for
thisin thefunction.
Return Value
- boolean
trueif all elements of the enumerable match the givenpredicate,falseotherwise.
Throws
- Exception ({ name: 'ArgumentError' })
predicateisnull.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 4])
console.log(enumerable.every((item) => item < 5)) // true
console.log(enumerable.every((item) => item < 4)) // falseImplements
IEnumerable.everyDefined in
IEnumerable.everyFills the elements in this list from the start to the end index with value.
Fills the elements in this list from the
start to the end index with value.This function mimics the JavaScript
Array.fill() function and returns this.final
Parameters
- value: T
- The value to fill the elements with
- start?: number
- The index of the first cell in the list to fill with the value.
0if not provided andsize+startif negative. - end?: number
- The index after the last cell in the list to fill with the value.
size-1if not provided andsize+endif negative.
Return Value
- List<T>
- The list itself for method chaining.
Returns a dynamic IEnumerable<T> of the elements of this enumerable which match the given predicate.
Returns a dynamic IEnumerable<T> of the elements of this enumerable which match the given
predicate.Parameters
- predicate: function(T, number, IEnumerable<T>): boolean
- A predicate function which returns
trueif the given element should be included in the resulting enumerable. - thisArg?: any
- The optional object to use for
thisin thefunction.
Return Value
- IEnumerable<T>
- A dynamic enumerable of the elements which match the given
predicate.
Examples
const enumerable = IEnumerable.from([10, 2, 30, 4, 5, 6])
const result = enumerable.filter((item) => item < 5)
console.log(result.toArray()) // yields [2,4]const list = List.fromArray([10, 2, 30, 4, 5, 6])
const result = list.filter((item) => item < 5)
list.add(1)
// result changed also
console.log(result.toArray()) // yields [2,4,1]Implements
IEnumerable.filterDefined in
IEnumerable.filterfilter
<TResult> (predicate: function(T, number, IEnumerable<T>): boolean, thisArg?: any): IEnumerable<TResult>Returns a dynamic IEnumerable<T> of the elements of this enumerable which match the given type guard.
filter
<TResult> (predicate: function(T, number, IEnumerable<T>): boolean, thisArg?: any): IEnumerable<TResult>Returns a dynamic IEnumerable<T> of the elements of this enumerable which match the given type guard.
Parameters
- predicate: function(T, number, IEnumerable<T>): boolean
A type predicate function which returns
trueif the given element is of the desired type and should be included in the resulting enumerable. The signature of the predicate must include a type guard, for examplefunction isFish(pet: Fish | Bird): pet is Fish { return (pet as Fish).swim !== undefined }- thisArg?: any
- The optional object to use for
thisin thefunction.
Return Value
- IEnumerable<TResult>
- A dynamic enumerable of the elements which match the given type guard.
Examples
const list = List.fromArray([1, 'a', 'b'])
list
.filter(
(item: string | number): item is string => typeof item === 'string',
)
.forEach((item: string) => console.log(item.toUpperCase())) // logs "A B"Defined in
IEnumerable.filterfilter
<TResult> (predicate: function(T, number, IEnumerable<T>): boolean, thisArg?: any): IEnumerable<T & TResult>Returns a dynamic IEnumerable<T> of the elements of this enumerable which match the given type guard.
filter
<TResult> (predicate: function(T, number, IEnumerable<T>): boolean, thisArg?: any): IEnumerable<T & TResult>Returns a dynamic IEnumerable<T> of the elements of this enumerable which match the given type guard.
Parameters
- predicate: function(T, number, IEnumerable<T>): boolean
A type predicate function which returns
trueif the given element is of the desired type and should be included in the resulting enumerable. The signature of the predicate must include a type guard, for examplefunction isFish(pet: Fish | Bird): pet is Fish { return (pet as Fish).swim !== undefined }- thisArg?: any
- The optional object to use for
thisin thefunction.
Return Value
- IEnumerable<T & TResult>
- A dynamic enumerable of the elements which match the given type guard.
Examples
const list = List.fromArray([1, 'a', 'b'])
list
.filter(
(item: string | number): item is string => typeof item === 'string',
)
.forEach((item: string) => console.log(item.toUpperCase())) // logs "A B"Defined in
IEnumerable.filterReturns the first element of the enumerable that matches a given predicate or null if there is no such element.
Returns the first element of the enumerable that matches a given
predicate or null if there is no such element.Parameters
- predicate: function(T, number, IEnumerable<T>): boolean
- A predicate function which returns
trueif the given element should be returned. - thisArg?: any
- The optional object to use for
thisin thefunction.
Return Value
- T
- The first element of this enumerable that matches the predicate or
nullif there is no such element.
Examples
const enumerable = IEnumerable.from([10, 2, 3, 9, 5, 6])
console.log(enumerable.find((item) => item < 5)) // yields 2
// no matches with the given predicate
console.log(enumerable.find((item) => item < 2)) // yields nullImplements
IEnumerable.findDefined in
IEnumerable.findSearches the enumerable for the first item for which predicate returns true and returns its index.
Searches the enumerable for the first item for which
predicate returns true and returns its index.Parameters
- predicate: function(T, number, IEnumerable<T>): boolean
- A predicate function which returns
trueif an item matches the search conditions. - thisArg?: any
- The optional object to use for
thisin thefunction.
Return Value
- number
- The index of the first item for which
predicatereturnstrueor -1 if there is no such item in the enumerable.
Implements
IEnumerable.findIndexDefined in
IEnumerable.findIndexReturns the last element of the enumerable that matches a given predicate or null if there is no such element.
Returns the last element of the enumerable that matches a given
predicate or null if there is no such element.Parameters
- predicate: function(T, number, IEnumerable<T>): boolean
- A predicate function which returns
trueif the given element should be returned. - thisArg?: any
- The optional object to use for
thisin thefunction.
Return Value
- T
- The last element of this enumerable that matches the predicate or
nullif there is no such element.
Examples
const enumerable = IEnumerable.from([10, 2, 3, 9, 5, 6])
console.log(enumerable.findLast((item) => item < 5)) // yields 3
// no matches with the given predicate
console.log(enumerable.findLast((item) => item < 2)) // yields nullImplements
IEnumerable.findLastDefined in
IEnumerable.findLastSearches the enumerable for the last item for which predicate returns true and returns its index.
Searches the enumerable for the last item for which
predicate returns true and returns its index.Parameters
- predicate: function(T, number, IEnumerable<T>): boolean
- A predicate function which returns
trueif an item matches the search conditions. - thisArg?: any
- The optional object to use for
thisin thefunction.
Return Value
- number
- The index of the last item for which
predicatereturnstrueor -1 if there is no such item in the enumerable.
Implements
IEnumerable.findLastIndexDefined in
IEnumerable.findLastIndexReturn Value
- T
- The first matching element of this enumerable or
nullif nothing was found.
Examples
const enumerable = IEnumerable.from([10, 2, 3, 9, 5, 6])
console.log(enumerable.first()) // yields 10
// returns null for empty enumerable
const empty = IEnumerable.from([])
console.log(empty.first()) // yields nullSee Also
API
- at
Implements
IEnumerable.firstDefined in
IEnumerable.firstflatMap
<TResult> (selector: function(T, number, IEnumerable<T>): IEnumerable<TResult>, thisArg?: any): IEnumerable<TResult>Returns a flattened dynamic IEnumerable<T> of this enumerable using the given selector function which returns an enumerable for each element of this enumerable.
flatMap
<TResult> (selector: function(T, number, IEnumerable<T>): IEnumerable<TResult>, thisArg?: any): IEnumerable<TResult>Returns a flattened dynamic IEnumerable<T> of this enumerable using the given
selector function which returns an enumerable for each element of this enumerable.Parameters
- selector: function(T, number, IEnumerable<T>): IEnumerable<TResult>
- A function which returns an enumerable for each element of this enumerable.
- thisArg?: any
- The optional object to use for
thisin thefunction.
Return Value
- IEnumerable<TResult>
- A flattened dynamic enumerable of the enumerables provided for each element of this enumerable.
Examples
const enumerable = IEnumerable.from<number>([1, 2, 3])
const result = enumerable.flatMap((item) =>
IEnumerable.from<number>([item, item * 2]),
)
console.log(result.toArray()) // yields [1,2,2,4,3,6]const list = List.fromArray<number[]>([[1], [2], [3]])
const result = list.flatMap((item) =>
IEnumerable.from<number>([item[0] * 2]),
)
list.add([4])
// result changed also
console.log(result.toArray()) // yields [2,4,6,8]Implements
IEnumerable.flatMapDefined in
IEnumerable.flatMapIterates this enumerable and invokes the given function for each element with the element, its index, and this enumerable as arguments.
Iterates this enumerable and invokes the given function for each element with the element, its index, and this enumerable as arguments.
Parameters
- action: function(T, number, IEnumerable<T>): void
- The function to call for each element.
- thisArg?: any
- The optional object to use for
thisin thefunction.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 4])
enumerable.forEach((item) => console.log(item)) // 1 2 3 4
// accessing the index
enumerable.forEach((item, index) =>
console.log(`Item #${index} is ${item}`),
)Implements
IEnumerable.forEachDefined in
IEnumerable.forEachParameters
- index: number
- The index to get the item for.
Return Value
- T
- The item at the given index.
Implements
IList.getGets an IEnumerator<T> which can be used to enumerate the items this instance represents.
Gets an IEnumerator<T> which can be used to enumerate the items this instance represents.
This is the low-level API that needs to be implemented by all enumerables. Clients will only have to use this API, rarely, as iteration is more conveniently handled by the forEach method or using the iterable (
for(let item of enumerable){}) protocol.final
Return Value
- IEnumerator<T>
- The IEnumerator<T> which can be used to iterate over the items in this instance
Implements
IEnumerable.getEnumeratorgroupBy
<TKey, TResult> (keySelector: function(T, number, IEnumerable<T>): TKey, resultCreator?: function(TKey, IEnumerable<T>, number): TResult): IEnumerable<TResult>Groups the elements in the enumerable according to the equality of a key.
groupBy
<TKey, TResult> (keySelector: function(T, number, IEnumerable<T>): TKey, resultCreator?: function(TKey, IEnumerable<T>, number): TResult): IEnumerable<TResult>Groups the elements in the enumerable according to the equality of a key.
For each element in the original enumerable the
keySelector will be used to retrieve a key. All elements with the same key will be provided to the optional resultCreator function that receives both the key, and an IEnumerable<T> of the elements that match that key to create a resulting object that will be yielded by this enumerable. Note that the resulting enumerable does not necessarily preserve the order of the (first) appearance of their elements in the original sequence. The elements of each group passed to the resultCreator are in the order of their original appearance.This implementation differs from the TC39 proposal for array grouping. See the example "Using groupBy to create an object" to mimic the proposal's groupBy behavior.
Parameters
- keySelector: function(T, number, IEnumerable<T>): TKey
- A function which selects from each element the key.
- resultCreator?: function(TKey, IEnumerable<T>, number): TResult
- An optional function which transforms each group of elements and their common key into a resulting object. If omitted, the default implementation will create arrays of length 2 where the first element is the key and the second element is an IEnumerable<TSource> of the elements with the corresponding key.
Return Value
- IEnumerable<TResult>
- An enumeration of groups of the original enumerable.
Examples
const enumerable = IEnumerable.from([
{ type: 'A', amount: 1 },
{ type: 'A', amount: 2 },
{ type: 'B', amount: 4 },
])
// without result creator
const groups1 = enumerable.groupBy<string, string>(
(element) => element.type,
)
for (const g of groups1) {
console.log(`Type is ${g[0]} and has ${g[1].length} members.`)
}
// Type is A and has 2 members.
// Type is B and has 1 members.
// with result creator
const groups2 = enumerable.groupBy(
(element) => element.type,
(type, elements) => ({
type,
amount: elements.sum((item) => item.amount),
}),
)
console.log(groups2.toArray()) // yields [{"type":"A","amount":4},{"type":"B","amount":4}]const val = IEnumerable.ofRange(1, 10)
.groupBy(
(value, index) => index % 2,
(key, items, index) => [`key ${index % 2}`, items.toArray()],
)
.toArray()
// [
// ["key 0",[1,3,5,7,9]],
// ["key 1",[2,4,6,8,10]]
// ]const enumerable: IEnumerable<{ value: number; key: string }> =
IEnumerable.from([
{ value: 0, key: 'a' },
{ value: 1, key: 'b' },
{ value: 2, key: 'b' },
{ value: 3, key: 'a' },
])
const obj = Object.fromEntries(
enumerable.groupBy(
(item) => item.key,
(key, items) => [key, items.toArray()],
) as Iterable<[unknown, unknown]>,
)
// {
// a: [{value:0, key:a}, {value:3, key:a}],
// b: [{value:1, key:b}, {value:2, key:b}]
// }Implements
IEnumerable.groupByDefined in
IEnumerable.groupByfinal
Parameters
- item: T
- The value to search for.
Return Value
- boolean
trueif this enumerable contains the given value,falseotherwise.
Implements
ICollection.includesParameters
- item: T
- The item to search for.
- fromIndex?: number
- An optional start index for the search.
Return Value
- number
- The index of the given item in the enumerable.
-1if the item is not in the enumerable.
Implements
IEnumerable.indexOfDefined in
IEnumerable.indexOffinal
Parameters
- index: number
- The index to insert the item at.
- item: T
- The item to insert.
Implements
IList.insertReturn Value
- T
- The last element of this enumerable, or
nullif the enumerable is empty.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 4])
console.log(enumerable.last()) // yields 4
// if the enumerable is empty null will be returned
const empty = IEnumerable.from([])
console.log(empty.last()) // yields nullSee Also
API
- at
Implements
IEnumerable.lastDefined in
IEnumerable.lastmap
<TResult> (selector: function(T, number, IEnumerable<T>): TResult, thisArg?: any): IEnumerable<TResult>Returns a dynamic IEnumerable<T> of this enumerable using the given selector function which returns a new object for each element of this enumerable.
map
<TResult> (selector: function(T, number, IEnumerable<T>): TResult, thisArg?: any): IEnumerable<TResult>Returns a dynamic IEnumerable<T> of this enumerable using the given
selector function which returns a new object for each element of this enumerable.Parameters
- selector: function(T, number, IEnumerable<T>): TResult
- A function which converts each element into a new element of the type
TResult. - thisArg?: any
- The optional value to use as
thiswhen executing theselector.
Return Value
- IEnumerable<TResult>
- A dynamic enumerable of the results of the provided function.
Examples
const enumerable = IEnumerable.from([1, 2, 3])
const result = enumerable.map((item) => item * 2)
console.log(result.toArray()) // yields [2, 4, 6]
// with index
const result2 = enumerable.map((item, index) => item * index)
console.log(result2.toArray()) // yields [0, 2, 6]
// with index and enumerable
const result3 = enumerable.map(
(item, index, enumerable) =>
`Item no ${index} of ${enumerable.size} is ${item}`,
)
console.log(result3.toArray())
// ["Item no 0 of 3 is 1","Item no 1 of 3 is 2","Item no 2 of 3 is 3"]const list = List.fromArray([1, 2, 3])
const result = list.map((item) => item * 2)
list.add(4)
// result changed also
console.log(result.toArray()) // yields [2,4,6,8]Implements
IEnumerable.mapDefined in
IEnumerable.mapConverts this untyped enumerable into an enumerable with the given type.
Converts this untyped enumerable into an enumerable with the given type.
The source enumerable may contain elements which are not of the given type. Those elements will not be contained in the returned enumerable.
Parameters
- resultType: typeof Number
- The type of the elements of the result enumerable.
Return Value
- IEnumerable<number>
- A typed enumerable.
Implements
IEnumerable.ofTypeDefined in
IEnumerable.ofTypeConverts this untyped enumerable into an enumerable with the given type.
Converts this untyped enumerable into an enumerable with the given type.
The source enumerable may contain elements which are not of the given type. Those elements will not be contained in the returned enumerable.
Parameters
- resultType: typeof String
- The type of the elements of the result enumerable.
Return Value
- IEnumerable<string>
- A typed enumerable.
Implements
IEnumerable.ofTypeDefined in
IEnumerable.ofTypeConverts this untyped enumerable into an enumerable with the given type.
Converts this untyped enumerable into an enumerable with the given type.
The source enumerable may contain elements which are not of the given type. Those elements will not be contained in the returned enumerable.
Parameters
- resultType: typeof Boolean
- The type of the elements of the result enumerable.
Return Value
- IEnumerable<boolean>
- A typed enumerable.
Implements
IEnumerable.ofTypeDefined in
IEnumerable.ofTypeConverts this untyped enumerable into an enumerable with the given type.
Converts this untyped enumerable into an enumerable with the given type.
The source enumerable may contain elements which are not of the given type. Those elements will not be contained in the returned enumerable.
Parameters
- resultType: Constructor<TResult>
- The type of the elements of the result enumerable.
Return Value
- IEnumerable<TResult>
- A typed enumerable.
Implements
IEnumerable.ofTypeDefined in
IEnumerable.ofTypeThis function mimics the JavaScript
Array.pop() function and returns undefined if the list is empty.Return Value
- T
- The removed value.
undefinedif the list was empty.
Defined in
IList.popCreates a wrapped enumerable that has on or several elements prepended to it.
Creates a wrapped enumerable that has on or several elements prepended to it.
Parameters
- items: T
- The item(s) to prepend to the enumeration
Return Value
- IEnumerable<T>
- A new live view over the original enumerable that has
itemsprepended to it.
Examples
const enumerable1 = IEnumerable.from([1, 2, 3, 4])
console.log(enumerable1.prepend(0).toArray()) // yields [0,1,2,3,4]
// more than one value can be used too
const enumerable2 = IEnumerable.from([3, 4, 5, 6])
console.log(enumerable2.prepend(1, 2).toArray()) // yields [1,2,3,4,5,6]See Also
API
- append
Implements
IEnumerable.prependDefined in
IEnumerable.prependThis function mimics the JavaScript
Array.push() function.Parameters
- values: T
- The values to append to the list.
Return Value
- number
- The new length of the list.
Defined in
IList.pushApplies the accumulator function to this elements of this enumerable.
Applies the
accumulator function to this elements of this enumerable.Parameters
- accumulator: function(T, T, number, IEnumerable<T>): T
- A function which "adds" (accumulates) a value depending on the element and index to the initial value and returns the result.
Return Value
- T
- The result of the accumulation.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 4, 5])
const result = enumerable.reduce(
(accumulator, currentValue) => accumulator + currentValue,
)
console.log(result) // expected output: 15See Also
API
- reduce, reduceRight
Implements
IEnumerable.reduceDefined in
IEnumerable.reducereduce
<TAccumulate> (accumulator: function(TAccumulate, T, number, IEnumerable<T>): TAccumulate, initialValue: TAccumulate): TAccumulateApplies the accumulator function to this elements of this enumerable.
reduce
<TAccumulate> (accumulator: function(TAccumulate, T, number, IEnumerable<T>): TAccumulate, initialValue: TAccumulate): TAccumulateApplies the
accumulator function to this elements of this enumerable.Parameters
- accumulator: function(TAccumulate, T, number, IEnumerable<T>): TAccumulate
- A function which "adds" (accumulates) a value depending on the element and index to the initial value and returns the result.
- initialValue: TAccumulate
- The initial value for the accumulator. Omit/use the reduce overload if you don't want to specify an initial value.
Return Value
- TAccumulate
- The result of the accumulation.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 4, 5])
const result = enumerable.reduce(
(accumulator, currentValue) => Math.max(accumulator, currentValue),
0,
)
console.log(result) // expected output: 5See Also
API
- reduceRight
Implements
IEnumerable.reduceDefined in
IEnumerable.reduceApplies the accumulator function to this elements of this enumerable in reverse order.
Applies the
accumulator function to this elements of this enumerable in reverse order.Parameters
- accumulator: function(T, T, number, IEnumerable<T>): T
- A function which "adds" (accumulates) a value depending on the element and index to the seed value and returns the result.
Return Value
- T
- The result of the accumulation.
Examples
const enumerable = IEnumerable.from([
[0, 1],
[2, 3],
[4, 5],
])
const result = enumerable.reduceRight((accumulator, currentValue) =>
accumulator.concat(currentValue),
)
console.log(result) // yields [4,5,2,3,0,1]See Also
API
- reduceRight, reduce
Implements
IEnumerable.reduceRightDefined in
IEnumerable.reduceRightreduceRight
<TAccumulate> (accumulator: function(TAccumulate, T, number, IEnumerable<T>): TAccumulate, initialValue: TAccumulate): TAccumulateApplies the accumulator function to this elements of this enumerable in reverse order.
reduceRight
<TAccumulate> (accumulator: function(TAccumulate, T, number, IEnumerable<T>): TAccumulate, initialValue: TAccumulate): TAccumulateApplies the
accumulator function to this elements of this enumerable in reverse order.Parameters
- accumulator: function(TAccumulate, T, number, IEnumerable<T>): TAccumulate
- A function which "adds" (accumulates) a value depending on the element and index to the seed value and returns the result.
- initialValue: TAccumulate
- The initial value for the accumulator. Omit/use the reduceRight overload if you don't want to specify an initial value.
Return Value
- TAccumulate
- The final value of the accumulation.
Examples
const enumerable = IEnumerable.from(['a', 'b', 'c', 'd'])
const result = enumerable.reduceRight(
(accumulator, currentValue) => currentValue + accumulator,
'.',
)
console.log(result) // yields 'abcd.'See Also
API
- reduceRight, reduce
Implements
IEnumerable.reduceRightDefined in
IEnumerable.reduceRightfinal
Parameters
- item: T
- The item to remove.
Return Value
- boolean
- Whether the item was removed from the collection.
Implements
ICollection.removefinal
Parameters
- match: function(T): boolean
- A predicate function with the signature
function(o:object):booleanwhich returnstrueif o matches the conditions of the element to search for.
Return Value
- number
- The number of elements which were removed.
final
Parameters
- index: number
- The index of the first element to remove.
- count: number
- The number of elements to remove.
Parameters
- index: number
- The index of the item to access.
- value: T
- The item at the given index.
Implements
IList.setThis function mimics the JavaScript
Array.shift() function and returns undefined if the list is empty.Return Value
- T
- The removed value.
undefinedif the list was empty.
Defined in
IList.shiftReturns a shallow copy of a portion of this list in the form of a new and independent list.
Returns a shallow copy of a portion of this list in the form of a new and independent list.
This function mimics the JavaScript
Array.slice() function, but returns a List<T> instead of an array.final
Parameters
- begin?: number
- The start index for the copy,
0if omitted - end?: number
- The index of the element after the last element that will be copied. If omitted, the
Return Value
- List<T>
- A new, shallow, independent copy of this list.
Determines whether this enumerable contains any elements matching the given predicate.
Determines whether this enumerable contains any elements matching the given
predicate.Parameters
- predicate?: function(T, number, IEnumerable<T>): boolean
- A function which returns
trueif the element matches a condition. - thisArg?: any
- The optional object to use for
thisin thefunction.
Return Value
- boolean
- Whether this enumerable contains any elements matching the given
predicate.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 4, 5, 6, 7])
const result = enumerable.some((item) => item % 2 === 0)
console.log(result) // trueImplements
IEnumerable.someDefined in
IEnumerable.somefinal
Parameters
- comparator?: function(T, T): number
- The optional comparison function to use. If no comparison function is provided, the elements are sorted by their natural order. Always provide a comparison function for lists containing a mixture of primitive values and other objects. Otherwise, the behavior is undefined.
Alters the contents of this list like JavaScript's Array.splice() method.
Alters the contents of this list like JavaScript's
Array.splice() method.This function mimics the JavaScript
Array.splice() function, but returns a List<T> instead of an array.final
Parameters
- start: number
- The index into the list at which elements should be removed and inserted.
- deleteCount?: number
- The number of items to remove starting at
start. If omitted all elements afterstartwill be removed. - items: T
- The items to insert. If omitted, no items will be added.
Return Value
Calculates the sum of the elements of this enumerable.
Calculates the sum of the elements of this enumerable.
Parameters
- selector: function(T, number, IEnumerable<T>): number
- A function which returns a numeric value for the given element.
Return Value
- number
- The sum of the elements of the enumerable.
Examples
const enumerable = IEnumerable.from([
{ type: 'A', amount: 3 },
{ type: 'B', amount: 2 },
{ type: 'C', amount: 2 },
{ type: 'D', amount: 5 },
])
const result = enumerable.sum((element) => element.amount)
console.log(result) // yields 12Implements
IEnumerable.sumDefined in
IEnumerable.sumCreates a dynamic view of this enumerable with the given number of elements taken from the start or end of this enumerable.
Creates a dynamic view of this enumerable with the given number of elements taken from the start or end of this enumerable.
Parameters
- count: number
- The number of elements in the created enumerable. If the number is positive, the elements are taken from the beginning, otherwise, the elements are taken from the end.
Return Value
- IEnumerable<T>
- A dynamic iew of this enumerable with the given number of elements.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 4, 5, 6, 7])
// positive parameter means taking elements from the beginning of the enumerable
console.log(enumerable.take(2).toArray()) // yields [1,2]
// negative parameter means taking elements from the end of the enumerable
console.log(enumerable.take(-2).toArray()) // yields [6,7]See Also
Implements
IEnumerable.takeDefined in
IEnumerable.takeReturns a dynamic IEnumerable<T> that contains the elements from this enumerable as long as the given predicate is true.
Returns a dynamic IEnumerable<T> that contains the elements from this enumerable as long as the given
predicate is true.Parameters
- predicate: function(T, number, IEnumerable<T>): boolean
- A function which returns
trueas long as the elements should be added to the returned enumerable.
Return Value
- IEnumerable<T>
- A dynamic enumerable that is a subset of the original enumerable.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 30, 4, 5])
const result = enumerable.takeWhile((item) => item < 5)
console.log(result.toArray()) // yields [1,2,3]Implements
IEnumerable.takeWhileDefined in
IEnumerable.takeWhileReturn Value
- T
- An array with the elements of this enumerable.
Implements
IEnumerable.toArrayDefined in
IEnumerable.toArrayReturns an dynamic enumerable which contains the elements of this enumerable in reverse order.
Returns an dynamic enumerable which contains the elements of this enumerable in reverse order.
Return Value
- IEnumerable<T>
- A dynamic enumerable which contains the elements of the original enumerable in reverse order.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 4, 5])
const result = enumerable.toReversed()
console.log(result.toArray()) // yields [5,4,3,2,1]const list = List.fromArray([1, 2, 3, 4, 5])
const result = list.toReversed()
list.add(0)
// result changed also
console.log(result.toArray()) // yields [0,5,4,3,2,1]Implements
IEnumerable.toReversedDefined in
IEnumerable.toReversedYields an ordered enumerable of the elements given an optional comparison function.
Yields an ordered enumerable of the elements given an optional comparison function.
The sorting will happen each time the enumerable is enumerated.
Parameters
- comparator?: function(T, T): number
- A function with the signature
(a, b) => numberwhich is used for the sort operation to compare the elements in the enumerable. May be omitted. If omitted, the elements are sorted by converting them to strings first. Numbers are sorted as numbers without converting them to strings (this is different to the EcmaScript implementation).
Return Value
- IEnumerable<T>
- A sorted enumeration of the original enumerable.
Examples
const enumerable = IEnumerable.from([
{ type: 'name-6', rank: 8 },
{ type: 'name-23', rank: 4 },
{ type: 'name-3', rank: 5 },
])
// order by the elements' rank
const sortedEnumerable1 = enumerable.toSorted(
(entryA, entryB) => entryA.rank - entryB.rank,
)
console.log(sortedEnumerable1.toArray())
// [{ "type": "name-23", "rank": 4 }, { "type": "name-3", "rank": 5 }, { "type": "name-6", "rank": 8 }]
// order by the elements' type using numeric collation to get a more "natural" order, with a comparer function
const collator = new Intl.Collator(undefined, {
numeric: true,
sensitivity: 'base',
})
const sortedEnumerable2 = enumerable.toSorted((entryA, entryB) =>
collator.compare(entryA.type, entryB.type),
)
console.log(sortedEnumerable2.toArray())
// [{ "type": "name-3", "rank": 5 }, { "type": "name-6", "rank": 8 },{ "type": "name-23", "rank": 4 }]Implements
IEnumerable.toSortedDefined in
IEnumerable.toSortedThis function mimics the JavaScript
Array.unshift() function.Parameters
- values: T
- The values to prepend to this list. The first argument will become the new first element in the list
Return Value
- number
- The new length of the list.
Defined in
IList.unshiftStatic Methods
Creates a new list similar in behavior to Array.from().
Creates a new list similar in behavior to
Array.from().static
Parameters
- enumerable: IEnumerable<TSource>
- The enumerable to fill the list with.
- mapFunction?: function(TSource, number, IEnumerable<TSource>): T
- The optional function to call for each element to map the generate the elements in the newly created list
- thisArg?: any
- The optional object to use for
thisin thefunction.