class Collection
extends
Mapdeclare class Collection<K, V> extends Map<K, V>
A Map with additional utility methods. This is used throughout discord.js rather than Arrays for anything that has an ID, for significantly improved performance and ease-of-use.
Type Parameters
K
V
atindex: number) : V | undefined (
Identical to [Array.at()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at). Returns the item at a given index, allowing for positive and negative integers. Negative integers count back from the last item in the collection.
cloneCollection<K, V> () :
Creates an identical shallow copy of this collection.
static combineEntriesK
V
>(entries: Iterable<[K, V]>combine: (firstValue: V, secondValue: V, key: K) => V) : Collection<K, V> <
K
V
Creates a Collection from a list of entries.
concatcollections: ReadonlyCollection<K, V>[]) : Collection<K, V> (
Combines this collection with others into a new collection. None of the source collections are modified.
differenceT
>(other: ReadonlyCollection<K, T>) : Collection<K, V | T> <
T
The difference method returns a new structure containing items where the key is present in one of the original structures but not the other.
Identical to [Map.forEach()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach), but returns the collection instead of undefined.
Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator.
equalscollection: ReadonlyCollection<K, V>) : boolean (
Checks if this collection shares identical items with another. This is different to checking for equality using equal-signs, because the collections may be different objects, but contain the same data.
Returns: Whether the collections have identical contents
everyK2 extends K
>(fn: (value: V, key: K, collection: this) => key is K2) : this is Collection<K2, V> <
K2 extends K
Checks if all items passes a test. Identical in behavior to [Array.every()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every).
filterK2 extends K
>(fn: (value: V, key: K, collection: this) => key is K2) : Collection<K2, V> <
K2 extends K
Identical to [Array.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter), but returns a Collection instead of an Array.
Searches for a single item where the given function returns a truthy value. This behaves like [Array.find()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find). All collections used in Discord.js are mapped using their id
property, and if you want to find by id you should use the get
method. See [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get) for details.
Searches for the key of a single item where the given function returns a truthy value. This behaves like [Array.findIndex()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex), but returns the key rather than the positional index.
flatMapT
>(fn: (value: V, key: K, collection: this) => Collection<K, T>) : Collection<K, T> <
T
Maps each item into a Collection, then joins the results into a single Collection. Identical in behavior to [Array.flatMap()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap).
hasAllkeys: K[]) : boolean (
Checks if all of the elements exist in the collection.
Returns: true
if all of the elements exist, false
if at least one does not exist.
hasAnykeys: K[]) : boolean (
Checks if any of the elements exist in the collection.
Returns: true
if any of the elements exist, false
if none exist.
intersectT
>(other: ReadonlyCollection<K, T>) : Collection<K, T> <
T
The intersect method returns a new structure containing items where the keys and values are present in both original structures.
keyAtindex: number) : K | undefined (
Identical to [Array.at()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at). Returns the key at a given index, allowing for positive and negative integers. Negative integers count back from the last item in the collection.
mapValuesT
>(fn: (value: V, key: K, collection: this) => T) : Collection<K, T> <
T
Maps each item to another value into a collection. Identical in behavior to [Array.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map).
mergeT
R
>(other: ReadonlyCollection<K, T>whenInSelf: (value: V, key: K) => Keep<R>whenInOther: (valueOther: T, key: K) => Keep<R>whenInBoth: (value: V, valueOther: T, key: K) => Keep<R>) : Collection<K, R> <
T
R
Merges two Collections together into a new Collection.
partitionK2 extends K
>(fn: (value: V, key: K, collection: this) => key is K2) : [Collection<K2, V>, Collection<Exclude<K, K2>, V>] <
K2 extends K
Partitions the collection into two collections where the first collection contains the items that passed and the second contains the items that failed.
Applies a function to produce a single value. Identical in behavior to [Array.reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce).
Identical to [Array.reverse()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse) but returns a Collection instead of an Array.
somefn: (value: V, key: K, collection: this) => boolean) : boolean (
Checks if there exists an item that passes a test. Identical in behavior to [Array.some()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some).
sortcompareFunction?: Comparator<K, V>) : this (
The sort method sorts the items of a collection in place and returns it. The sort is not necessarily stable in Node 10 or older. The default sort order is according to string Unicode code points.
sortedcompareFunction?: Comparator<K, V>) : Collection<K, V> (
The sorted method sorts the items of a collection and returns it. The sort is not necessarily stable in Node 10 or older. The default sort order is according to string Unicode code points.
sweepfn: (value: V, key: K, collection: this) => boolean) : number (
Removes items that satisfy the provided filter function.
Returns: The number of removed entries