Collection
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.
export declare class Collection<K, V> extends Map<K, V>
Extends
Map
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.
Creates a Collection from a list of entries.
Example
Collection.combineEntries([["a", 1], ["b", 2], ["a", 2]], (x, y) = x + y); // returns Collection "a" = 3, "b" = 2
Combines this collection with others into a new collection. None of the source collections are modified.
Example
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.
Example
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.
Example
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.
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
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.
Example
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().
Example
collection.flatMap(guild = guild.members.cache);
The intersect method returns a new structure containing items where the keys and values are present in both original structures.
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.
Example
collection.map(user = user.tag);
Merges two Collections together into a new Collection.
Example
// 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 ), );
Example
// 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.
Example
const [big, small] = collection.partition(guild = guild.memberCount 250);
Example
collection.reduce((acc, guild) = acc + guild.memberCount, 0);
Example
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.
Example
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.
Example
collection.sorted((userA, userB) = userA.createdTimestamp - userB.createdTimestamp);
Runs a function on the collection and returns the collection.
Example
collection .tap(coll = console.log(coll.size)) .filter(user = user.bot) .tap(coll = console.log(coll.size))