The key type this collection holds
The value type this collection holds
Optional
options: LimitedCollectionOptions<Key, Value>Optional
iterable: Iterable<readonly [Key, Value]>Readonly
[toReadonly
sizethe number of elements in the Map.
Static
Readonly
[species]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.
The index of the element to obtain
Creates an identical shallow copy of this collection.
const newColl = someColl.clone();
Combines this collection with others into a new collection. None of the source collections are modified.
Rest
...collections: ReadonlyCollection<Key, Value>[]Collections to merge
const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
The difference method returns a new structure containing items where the key is present in one of the original structures but not the other.
The other Collection to filter against
Identical to Map.forEach(), but returns the collection instead of undefined.
Function to execute for each element
collection
.each(user => console.log(user.username))
.filter(user => user.bot)
.each(user => console.log(user.username));
Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator.
The key to get if it exists, or set otherwise
A function that generates the default value
collection.ensure(guildId, () => defaultGuildConfig);
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.
Collection to compare with
Whether the collections have identical contents
Checks if all items passes a test. Identical in behavior to Array.every().
Function used to test (should return a boolean)
collection.every(user => !user.bot);
Identical to Array.filter(), but returns a Collection instead of an Array.
The function to test with (should return boolean)
collection.filter(user => user.username === 'Bob');
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.
The function to test with (should return boolean)
collection.find(user => user.username === 'Bob');
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.
The function to test with (should return boolean)
collection.findKey(user => user.username === 'Bob');
Maps each item into a Collection, then joins the results into a single Collection. Identical in behavior to Array.flatMap().
Function that produces a new Collection
collection.flatMap(guild => guild.members.cache);
Executes a provided function once per each key/value pair in the Map, in insertion order.
Optional
thisArg: anyReturns a specified element from the Map object. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map.
Returns the element associated with the specified key. If no element is associated with the specified key, undefined is returned.
The intersect method returns a new structure containing items where the keys and values are present in both original structures.
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.
The index of the key to obtain
Maps each item to another value into an array. Identical in behavior to Array.map().
Function that produces an element of the new array, taking three arguments
collection.map(user => user.tag);
Maps each item to another value into a collection. Identical in behavior to Array.map().
Function that produces an element of the new collection, taking three arguments
collection.mapValues(user => user.tag);
Merges two Collections together into a new Collection.
The other Collection to merge with
Function getting the result if the entry only exists in this Collection
Function getting the result if the entry only exists in the other Collection
Function getting the result if the entry exists in both Collections
// Sums up the entries in two collections.
coll.merge(
other,
x => ({ keep: true, value: x }),
y => ({ keep: true, value: y }),
(x, y) => ({ keep: true, value: x + y }),
);
// Intersects two collections in a left-biased manner.
coll.merge(
other,
x => ({ keep: false }),
y => ({ keep: false }),
(x, _) => ({ keep: true, value: x }),
);
Partitions the collection into two collections where the first collection contains the items that passed and the second contains the items that failed.
Function used to test (should return a boolean)
const [big, small] = collection.partition(guild => guild.memberCount > 250);
Applies a function to produce a single value. Identical in behavior to Array.reduce().
Function used to reduce, taking four arguments; accumulator
, currentValue
, currentKey
,
and collection
Optional
initialValue: TStarting value for the accumulator
collection.reduce((acc, guild) => acc + guild.memberCount, 0);
Identical to Array.reverse() but returns a Collection instead of an Array.
Adds a new element with a specified key and value to the Map. If an element with the same key already exists, the element will be updated.
Checks if there exists an item that passes a test. Identical in behavior to Array.some().
Function used to test (should return a boolean)
collection.some(user => user.discriminator === '0000');
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.
Optional
compareFunction: Comparator<Key, Value>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.
collection.sort((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
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.
Optional
compareFunction: Comparator<Key, Value>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.
collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
The subtract method returns a new structure containing items where the keys and values of the original structure are not present in the other.
The other Collection to filter against
Removes items that satisfy the provided filter function.
Function used to test (should return a boolean)
The number of removed entries
Runs a function on the collection and returns the collection.
Function to execute
collection
.tap(coll => console.log(coll.size))
.filter(user => user.bot)
.tap(coll => console.log(coll.size))
Static
combineCreates a Collection from a list of entries.
The list of entries
Function to combine an existing entry with a new one
Collection.combineEntries([["a", 1], ["b", 2], ["a", 2]], (x, y) => x + y);
// returns Collection { "a" => 3, "b" => 2 }
Generated using TypeDoc
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.