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.
Extends
Map<Key, Value>Name | Constraints | Optional | Default | Description |
---|---|---|---|---|
Key | No | The key type this collection holds | ||
Value | No | The value type this collection holds |
Identical to 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.
Name | Type | Optional | Description |
---|---|---|---|
index | number | No | The index of the element to obtain |
Creates an identical shallow copy of this collection.
Example
Creates a Collection from a list of entries.
Example
Name | Constraints | Optional | Default | Description |
---|---|---|---|---|
Key | No | None | ||
Value | No | None |
Name | Type | Optional | Description |
---|---|---|---|
entries | Iterable<[Key, Value]> | No | The list of entries |
combine | (firstValue: Value, secondValue: Value, key: Key) => Value | No | Function to combine an existing entry with a new one |
Combines this collection with others into a new collection. None of the source collections are modified.
Example
Name | Type | Optional | Description |
---|---|---|---|
...collections | ReadonlyCollection<Key, Value>[] | No | Collections to merge |
Returns a new collection containing the items where the key is present in this collection but not the other.
Example
Name | Type | Optional | Description |
---|---|---|---|
other | ReadonlyCollection<Key, any> | No | The other Collection to filter against |
Identical to Map.forEach(), but returns the collection instead of undefined.
Example
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: Value, key: Key, collection: this) => void | No | Function to execute for each element |
Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator.
Example
Name | Type | Optional | Description |
---|---|---|---|
key | Key | No | The key to get if it exists, or set otherwise |
defaultValueGenerator | (key: Key, collection: this) => Value | No | A function that generates the default value |
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 contentsName | Type | Optional | Description |
---|---|---|---|
collection | ReadonlyCollection<Key, Value> | No | Collection to compare with |
Checks if all items passes a test. Identical in behavior to Array.every().
Example
Name | Constraints | Optional | Default | Description |
---|---|---|---|---|
NewKey | Key | No | None |
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: Value, key: Key, collection: this) => key is NewKey | No | Function used to test (should return a boolean) |
Identical to Array.filter(), but returns a Collection instead of an Array.
Example
Name | Constraints | Optional | Default | Description |
---|---|---|---|---|
NewKey | Key | No | None |
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: Value, key: Key, collection: this) => key is NewKey | No | The function to test with (should return a boolean) |
Searches for a single item where the given function returns a truthy value. This behaves like 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 for details.Example
Name | Constraints | Optional | Default | Description |
---|---|---|---|---|
NewValue | Value | No | None |
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: Value, key: Key, collection: this) => value is NewValue | No | The function to test with (should return a boolean) |
Searches for the key of a single item where the given function returns a truthy value. This behaves like Array.findIndex(), but returns the key rather than the positional index.
Example
Name | Constraints | Optional | Default | Description |
---|---|---|---|---|
NewKey | Key | No | None |
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: Value, key: Key, collection: this) => key is NewKey | No | The function to test with (should return a boolean) |
Searches for a last item where the given function returns a truthy value. This behaves like Array.findLast().
Name | Constraints | Optional | Default | Description |
---|---|---|---|---|
NewValue | Value | No | None |
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: Value, key: Key, collection: this) => value is NewValue | No | The function to test with (should return a boolean) |
Searches for the key of a last item where the given function returns a truthy value. This behaves like Array.findLastIndex(), but returns the key rather than the positional index.
Name | Constraints | Optional | Default | Description |
---|---|---|---|---|
NewKey | Key | No | None |
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: Value, key: Key, collection: this) => key is NewKey | No | The function to test with (should return a boolean) |
Obtains the first value(s) in this collection.
Returns
A single value if no amount is provided or an array of values, starting from the end if amount is negativeObtains the first key(s) in this collection.
Returns
A single key if no amount is provided or an array of keys, starting from the end if amount is negativeMaps each item into a Collection, then joins the results into a single Collection. Identical in behavior to Array.flatMap().
Example
Name | Constraints | Optional | Default | Description |
---|---|---|---|---|
NewValue | No | None |
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: Value, key: Key, collection: this) => Collection<Key, NewValue> | No | Function that produces a new Collection |
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.Name | Type | Optional | Description |
---|---|---|---|
...keys | Key[] | No | The keys of the elements to check for |
Checks if any of the elements exist in the collection.
Returns
true
if any of the elements exist, false
if none exist.Name | Type | Optional | Description |
---|---|---|---|
...keys | Key[] | No | The keys of the elements to check for |
The intersection method returns a new collection containing the items where the key is present in both collections.
Example
Name | Type | Optional | Description |
---|---|---|---|
other | ReadonlyCollection<Key, any> | No | The other Collection to filter against |
Identical to 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.
Name | Type | Optional | Description |
---|---|---|---|
index | number | No | The index of the key to obtain |
Obtains the last value(s) in this collection.
Returns
A single value if no amount is provided or an array of values, starting from the start if amount is negativeObtains the last key(s) in this collection.
Returns
A single key if no amount is provided or an array of keys, starting from the start if amount is negativeMaps each item to another value into an array. Identical in behavior to Array.map().
Example
Name | Constraints | Optional | Default | Description |
---|---|---|---|---|
NewValue | No | None |
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: Value, key: Key, collection: this) => NewValue | No | Function that produces an element of the new array, taking three arguments |
Maps each item to another value into a collection. Identical in behavior to Array.map().
Example
Name | Constraints | Optional | Default | Description |
---|---|---|---|---|
NewValue | No | None |
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: Value, key: Key, collection: this) => NewValue | No | Function that produces an element of the new collection, taking three arguments |
Merges two Collections together into a new Collection.
Example
Example
Name | Constraints | Optional | Default | Description |
---|---|---|---|---|
OtherValue | No | None | ||
ResultValue | No | None |
Name | Type | Optional | Description |
---|---|---|---|
other | ReadonlyCollection<Key, OtherValue> | No | The other Collection to merge with |
whenInSelf | (value: Value, key: Key) => Keep<ResultValue> | No | Function getting the result if the entry only exists in this Collection |
whenInOther | (valueOther: OtherValue, key: Key) => Keep<ResultValue> | No | Function getting the result if the entry only exists in the other Collection |
whenInBoth | (value: Value, valueOther: OtherValue, key: Key) => Keep<ResultValue> | No | Function getting the result if the entry exists in both Collections |
Partitions the collection into two collections where the first collection contains the items that passed and the second contains the items that failed.
Example
Name | Constraints | Optional | Default | Description |
---|---|---|---|---|
NewKey | Key | No | None |
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: Value, key: Key, collection: this) => key is NewKey | No | Function used to test (should return a boolean) |
Obtains unique random value(s) from this collection.
Returns
A single value if no amount is provided or an array of valuesObtains unique random key(s) from this collection.
Returns
A single key if no amount is provided or an arrayApplies a function to produce a single value. Identical in behavior to Array.reduce().
Example
Name | Type | Optional | Description |
---|---|---|---|
fn | (accumulator: Value, value: Value, key: Key, collection: this) => Value | No | Function used to reduce, taking four arguments; accumulator , currentValue , currentKey , and collection |
initialValue | Value | Yes | Starting value for the accumulator |
Applies a function to produce a single value. Identical in behavior to Array.reduceRight().
Name | Type | Optional | Description |
---|---|---|---|
fn | (accumulator: Value, value: Value, key: Key, collection: this) => Value | No | Function used to reduce, taking four arguments; accumulator , value , key , and collection |
initialValue | Value | Yes | Starting value for the accumulator |
Identical to Array.reverse() but returns a Collection instead of an Array.
Checks if there exists an item that passes a test. Identical in behavior to Array.some().
Example
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: Value, key: Key, collection: this) => unknown | No | Function used to test (should return a boolean) |
The sort method sorts the items of a collection in place and returns it. The default sort order is according to string Unicode code points.
Example
Name | Type | Optional | Description |
---|---|---|---|
compareFunction | Comparator<Key, Value> | Yes | Specifies a function that defines the sort order. If omitted, the collection is sorted according to each character's Unicode code point value, according to the string conversion of each element. |
Removes items that satisfy the provided filter function.
Returns
The number of removed entriesName | Type | Optional | Description |
---|---|---|---|
fn | (value: Value, key: Key, collection: this) => unknown | No | Function used to test (should return a boolean) |
Returns a new collection containing only the items where the keys are present in either collection, but not both.
Example
Name | Constraints | Optional | Default | Description |
---|---|---|---|---|
OtherValue | No | None |
Name | Type | Optional | Description |
---|---|---|---|
other | ReadonlyCollection<Key, OtherValue> | No | The other Collection to filter against |
Runs a function on the collection and returns the collection.
Example
Name | Type | Optional | Description |
---|---|---|---|
fn | (collection: this) => void | No | Function to execute |
Identical to Array.toReversed() but returns a Collection instead of an Array.
The sorted method sorts the items of a collection and returns it. The default sort order is according to string Unicode code points.
Example
Name | Type | Optional | Description |
---|---|---|---|
compareFunction | Comparator<Key, Value> | Yes | Specifies a function that defines the sort order. If omitted, the collection is sorted according to each character's Unicode code point value, according to the string conversion of each element. |
Returns a new collection containing the items where the key is present in either of the collections.
Remarks
If the collections have any items with the same key, the value from the first collection will be used.Example
Name | Constraints | Optional | Default | Description |
---|---|---|---|---|
OtherValue | No | None |
Name | Type | Optional | Description |
---|---|---|---|
other | ReadonlyCollection<Key, OtherValue> | No | The other Collection to filter against |