Skip to main content

Class: Collection<K, V>

connect.Collection

Type parameters

Name
K
V

Hierarchy

  • Collection<K, V>

    Collection

Constructors

constructor

new Collection<K, V>(entries?)

Type parameters

Name
K
V

Parameters

NameType
entries?readonly readonly [K, V][]

Inherited from

BaseCollection<K, V>.constructor

new Collection<K, V>(iterable?)

Type parameters

Name
K
V

Parameters

NameType
iterable?Iterable<readonly [K, V]>

Inherited from

BaseCollection<K, V>.constructor

Properties

[toStringTag]

Readonly [toStringTag]: string

Inherited from

BaseCollection.__@toStringTag@22445


constructor

constructor: CollectionConstructor

Inherited from

BaseCollection.constructor


size

Readonly size: number

Inherited from

BaseCollection.size


[species]

Static Readonly [species]: MapConstructor

Inherited from

BaseCollection.__@species@22790


default

Static Readonly default: typeof Collection

Inherited from

BaseCollection.default

Methods

[iterator]

[iterator](): IterableIterator<[K, V]>

Returns an iterable of entries in the map.

Returns

IterableIterator<[K, V]>

Inherited from

BaseCollection.__@iterator@21310


at

at(index): V

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.

Parameters

NameTypeDescription
indexnumberThe index of the element to obtain

Returns

V

Inherited from

BaseCollection.at


clear

clear(): void

Returns

void

Inherited from

BaseCollection.clear


clone

clone(): Collection<K, V>

Creates an identical shallow copy of this collection.

Example

const newColl = someColl.clone();

Returns

Collection<K, V>

Inherited from

BaseCollection.clone


concat

concat(...collections): Collection<K, V>

Combines this collection with others into a new collection. None of the source collections are modified.

Example

const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);

Parameters

NameTypeDescription
...collectionsReadonlyCollection<K, V>[]Collections to merge

Returns

Collection<K, V>

Inherited from

BaseCollection.concat


delete

delete(key): boolean

Parameters

NameType
keyK

Returns

boolean

Inherited from

BaseCollection.delete


difference

difference<T>(other): Collection<K, V | 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.

Type parameters

Name
T

Parameters

NameTypeDescription
otherReadonlyCollection<K, T>The other Collection to filter against

Returns

Collection<K, V | T>

Inherited from

BaseCollection.difference


each

each(fn): Collection<K, V>

Identical to Map.forEach(), but returns the collection instead of undefined.

Example

collection
.each(user => console.log(user.username))
.filter(user => user.bot)
.each(user => console.log(user.username));

Parameters

NameTypeDescription
fn(value: V, key: K, collection: Collection<K, V>) => voidFunction to execute for each element

Returns

Collection<K, V>

Inherited from

BaseCollection.each

each<T>(fn, thisArg): Collection<K, V>

Type parameters

Name
T

Parameters

NameType
fn(this: T, value: V, key: K, collection: Collection<K, V>) => void
thisArgT

Returns

Collection<K, V>

Inherited from

BaseCollection.each


ensure

ensure(key, defaultValueGenerator): V

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);

Parameters

NameTypeDescription
keyKThe key to get if it exists, or set otherwise
defaultValueGenerator(key: K, collection: Collection<K, V>) => VA function that generates the default value

Returns

V

Inherited from

BaseCollection.ensure


entries

entries(): IterableIterator<[K, V]>

Returns an iterable of key, value pairs for every entry in the map.

Returns

IterableIterator<[K, V]>

Inherited from

BaseCollection.entries


equals

equals(collection): 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.

Parameters

NameTypeDescription
collectionReadonlyCollection<K, V>Collection to compare with

Returns

boolean

Whether the collections have identical contents

Inherited from

BaseCollection.equals


every

every<K2>(fn): this is Collection<K2, V>

Checks if all items passes a test. Identical in behavior to Array.every().

Example

collection.every(user => !user.bot);

Type parameters

Name
K2

Parameters

NameTypeDescription
fn(value: V, key: K, collection: Collection<K, V>) => key is K2Function used to test (should return a boolean)

Returns

this is Collection<K2, V>

Inherited from

BaseCollection.every

every<V2>(fn): this is Collection<K, V2>

Type parameters

Name
V2

Parameters

NameType
fn(value: V, key: K, collection: Collection<K, V>) => value is V2

Returns

this is Collection<K, V2>

Inherited from

BaseCollection.every

every(fn): boolean

Parameters

NameType
fn(value: V, key: K, collection: Collection<K, V>) => boolean

Returns

boolean

Inherited from

BaseCollection.every

every<This, K2>(fn, thisArg): this is Collection<K2, V>

Type parameters

Name
This
K2

Parameters

NameType
fn(this: This, value: V, key: K, collection: Collection<K, V>) => key is K2
thisArgThis

Returns

this is Collection<K2, V>

Inherited from

BaseCollection.every

every<This, V2>(fn, thisArg): this is Collection<K, V2>

Type parameters

Name
This
V2

Parameters

NameType
fn(this: This, value: V, key: K, collection: Collection<K, V>) => value is V2
thisArgThis

Returns

this is Collection<K, V2>

Inherited from

BaseCollection.every

every<This>(fn, thisArg): boolean

Type parameters

Name
This

Parameters

NameType
fn(this: This, value: V, key: K, collection: Collection<K, V>) => boolean
thisArgThis

Returns

boolean

Inherited from

BaseCollection.every


filter

filter<K2>(fn): Collection<K2, V>

Identical to Array.filter(), but returns a Collection instead of an Array.

Example

collection.filter(user => user.username === 'Bob');

Type parameters

Name
K2

Parameters

NameTypeDescription
fn(value: V, key: K, collection: Collection<K, V>) => key is K2The function to test with (should return boolean)

Returns

Collection<K2, V>

Inherited from

BaseCollection.filter

filter<V2>(fn): Collection<K, V2>

Type parameters

Name
V2

Parameters

NameType
fn(value: V, key: K, collection: Collection<K, V>) => value is V2

Returns

Collection<K, V2>

Inherited from

BaseCollection.filter

filter(fn): Collection<K, V>

Parameters

NameType
fn(value: V, key: K, collection: Collection<K, V>) => boolean

Returns

Collection<K, V>

Inherited from

BaseCollection.filter

filter<This, K2>(fn, thisArg): Collection<K2, V>

Type parameters

Name
This
K2

Parameters

NameType
fn(this: This, value: V, key: K, collection: Collection<K, V>) => key is K2
thisArgThis

Returns

Collection<K2, V>

Inherited from

BaseCollection.filter

filter<This, V2>(fn, thisArg): Collection<K, V2>

Type parameters

Name
This
V2

Parameters

NameType
fn(this: This, value: V, key: K, collection: Collection<K, V>) => value is V2
thisArgThis

Returns

Collection<K, V2>

Inherited from

BaseCollection.filter

filter<This>(fn, thisArg): Collection<K, V>

Type parameters

Name
This

Parameters

NameType
fn(this: This, value: V, key: K, collection: Collection<K, V>) => boolean
thisArgThis

Returns

Collection<K, V>

Inherited from

BaseCollection.filter


find

find<V2>(fn): V2

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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get) for details.

Example

collection.find(user => user.username === 'Bob');

Type parameters

Name
V2

Parameters

NameTypeDescription
fn(value: V, key: K, collection: Collection<K, V>) => value is V2The function to test with (should return boolean)

Returns

V2

Inherited from

BaseCollection.find

find(fn): V

Parameters

NameType
fn(value: V, key: K, collection: Collection<K, V>) => boolean

Returns

V

Inherited from

BaseCollection.find

find<This, V2>(fn, thisArg): V2

Type parameters

Name
This
V2

Parameters

NameType
fn(this: This, value: V, key: K, collection: Collection<K, V>) => value is V2
thisArgThis

Returns

V2

Inherited from

BaseCollection.find

find<This>(fn, thisArg): V

Type parameters

Name
This

Parameters

NameType
fn(this: This, value: V, key: K, collection: Collection<K, V>) => boolean
thisArgThis

Returns

V

Inherited from

BaseCollection.find


findKey

findKey<K2>(fn): K2

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');

Type parameters

Name
K2

Parameters

NameTypeDescription
fn(value: V, key: K, collection: Collection<K, V>) => key is K2The function to test with (should return boolean)

Returns

K2

Inherited from

BaseCollection.findKey

findKey(fn): K

Parameters

NameType
fn(value: V, key: K, collection: Collection<K, V>) => boolean

Returns

K

Inherited from

BaseCollection.findKey

findKey<This, K2>(fn, thisArg): K2

Type parameters

Name
This
K2

Parameters

NameType
fn(this: This, value: V, key: K, collection: Collection<K, V>) => key is K2
thisArgThis

Returns

K2

Inherited from

BaseCollection.findKey

findKey<This>(fn, thisArg): K

Type parameters

Name
This

Parameters

NameType
fn(this: This, value: V, key: K, collection: Collection<K, V>) => boolean
thisArgThis

Returns

K

Inherited from

BaseCollection.findKey


first

first(): V

Obtains the first value(s) in this collection.

Returns

V

A single value if no amount is provided or an array of values, starting from the end if amount is negative

Inherited from

BaseCollection.first

first(amount): V[]

Parameters

NameType
amountnumber

Returns

V[]

Inherited from

BaseCollection.first


firstKey

firstKey(): K

Obtains the first key(s) in this collection.

Returns

K

A single key if no amount is provided or an array of keys, starting from the end if amount is negative

Inherited from

BaseCollection.firstKey

firstKey(amount): K[]

Parameters

NameType
amountnumber

Returns

K[]

Inherited from

BaseCollection.firstKey


flatMap

flatMap<T>(fn): Collection<K, T>

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);

Type parameters

Name
T

Parameters

NameTypeDescription
fn(value: V, key: K, collection: Collection<K, V>) => Collection<K, T>Function that produces a new Collection

Returns

Collection<K, T>

Inherited from

BaseCollection.flatMap

flatMap<T, This>(fn, thisArg): Collection<K, T>

Type parameters

Name
T
This

Parameters

NameType
fn(this: This, value: V, key: K, collection: Collection<K, V>) => Collection<K, T>
thisArgThis

Returns

Collection<K, T>

Inherited from

BaseCollection.flatMap


forEach

forEach(callbackfn, thisArg?): void

Parameters

NameType
callbackfn(value: V, key: K, map: Map<K, V>) => void
thisArg?any

Returns

void

Inherited from

BaseCollection.forEach


get

get(key): V

Parameters

NameType
keyK

Returns

V

Inherited from

BaseCollection.get


has

has(key): boolean

Parameters

NameType
keyK

Returns

boolean

Inherited from

BaseCollection.has


hasAll

hasAll(...keys): boolean

Checks if all of the elements exist in the collection.

Parameters

NameTypeDescription
...keysK[]The keys of the elements to check for

Returns

boolean

true if all of the elements exist, false if at least one does not exist.

Inherited from

BaseCollection.hasAll


hasAny

hasAny(...keys): boolean

Checks if any of the elements exist in the collection.

Parameters

NameTypeDescription
...keysK[]The keys of the elements to check for

Returns

boolean

true if any of the elements exist, false if none exist.

Inherited from

BaseCollection.hasAny


intersect

intersect<T>(other): Collection<K, T>

The intersect method returns a new structure containing items where the keys and values are present in both original structures.

Type parameters

Name
T

Parameters

NameTypeDescription
otherReadonlyCollection<K, T>The other Collection to filter against

Returns

Collection<K, T>

Inherited from

BaseCollection.intersect


keyAt

keyAt(index): K

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.

Parameters

NameTypeDescription
indexnumberThe index of the key to obtain

Returns

K

Inherited from

BaseCollection.keyAt


keys

keys(): IterableIterator<K>

Returns an iterable of keys in the map

Returns

IterableIterator<K>

Inherited from

BaseCollection.keys


last

last(): V

Obtains the last value(s) in this collection.

Returns

V

A single value if no amount is provided or an array of values, starting from the start if amount is negative

Inherited from

BaseCollection.last

last(amount): V[]

Parameters

NameType
amountnumber

Returns

V[]

Inherited from

BaseCollection.last


lastKey

lastKey(): K

Obtains the last key(s) in this collection.

Returns

K

A single key if no amount is provided or an array of keys, starting from the start if amount is negative

Inherited from

BaseCollection.lastKey

lastKey(amount): K[]

Parameters

NameType
amountnumber

Returns

K[]

Inherited from

BaseCollection.lastKey


map

map<T>(fn): T[]

Maps each item to another value into an array. Identical in behavior to Array.map().

Example

collection.map(user => user.tag);

Type parameters

Name
T

Parameters

NameTypeDescription
fn(value: V, key: K, collection: Collection<K, V>) => TFunction that produces an element of the new array, taking three arguments

Returns

T[]

Inherited from

BaseCollection.map

map<This, T>(fn, thisArg): T[]

Type parameters

Name
This
T

Parameters

NameType
fn(this: This, value: V, key: K, collection: Collection<K, V>) => T
thisArgThis

Returns

T[]

Inherited from

BaseCollection.map


mapValues

mapValues<T>(fn): Collection<K, T>

Maps each item to another value into a collection. Identical in behavior to Array.map().

Example

collection.mapValues(user => user.tag);

Type parameters

Name
T

Parameters

NameTypeDescription
fn(value: V, key: K, collection: Collection<K, V>) => TFunction that produces an element of the new collection, taking three arguments

Returns

Collection<K, T>

Inherited from

BaseCollection.mapValues

mapValues<This, T>(fn, thisArg): Collection<K, T>

Type parameters

Name
This
T

Parameters

NameType
fn(this: This, value: V, key: K, collection: Collection<K, V>) => T
thisArgThis

Returns

Collection<K, T>

Inherited from

BaseCollection.mapValues


partition

partition<K2>(fn): [Collection<K2, V>, Collection<Exclude<K, K2>, V>]

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);

Type parameters

Name
K2

Parameters

NameTypeDescription
fn(value: V, key: K, collection: Collection<K, V>) => key is K2Function used to test (should return a boolean)

Returns

[Collection<K2, V>, Collection<Exclude<K, K2>, V>]

Inherited from

BaseCollection.partition

partition<V2>(fn): [Collection<K, V2>, Collection<K, Exclude<V, V2>>]

Type parameters

Name
V2

Parameters

NameType
fn(value: V, key: K, collection: Collection<K, V>) => value is V2

Returns

[Collection<K, V2>, Collection<K, Exclude<V, V2>>]

Inherited from

BaseCollection.partition

partition(fn): [Collection<K, V>, Collection<K, V>]

Parameters

NameType
fn(value: V, key: K, collection: Collection<K, V>) => boolean

Returns

[Collection<K, V>, Collection<K, V>]

Inherited from

BaseCollection.partition

partition<This, K2>(fn, thisArg): [Collection<K2, V>, Collection<Exclude<K, K2>, V>]

Type parameters

Name
This
K2

Parameters

NameType
fn(this: This, value: V, key: K, collection: Collection<K, V>) => key is K2
thisArgThis

Returns

[Collection<K2, V>, Collection<Exclude<K, K2>, V>]

Inherited from

BaseCollection.partition

partition<This, V2>(fn, thisArg): [Collection<K, V2>, Collection<K, Exclude<V, V2>>]

Type parameters

Name
This
V2

Parameters

NameType
fn(this: This, value: V, key: K, collection: Collection<K, V>) => value is V2
thisArgThis

Returns

[Collection<K, V2>, Collection<K, Exclude<V, V2>>]

Inherited from

BaseCollection.partition

partition<This>(fn, thisArg): [Collection<K, V>, Collection<K, V>]

Type parameters

Name
This

Parameters

NameType
fn(this: This, value: V, key: K, collection: Collection<K, V>) => boolean
thisArgThis

Returns

[Collection<K, V>, Collection<K, V>]

Inherited from

BaseCollection.partition


random

random(): V

Obtains unique random value(s) from this collection.

Returns

V

A single value if no amount is provided or an array of values

Inherited from

BaseCollection.random

random(amount): V[]

Parameters

NameType
amountnumber

Returns

V[]

Inherited from

BaseCollection.random


randomKey

randomKey(): K

Obtains unique random key(s) from this collection.

Returns

K

A single key if no amount is provided or an array

Inherited from

BaseCollection.randomKey

randomKey(amount): K[]

Parameters

NameType
amountnumber

Returns

K[]

Inherited from

BaseCollection.randomKey


reduce

reduce<T>(fn, initialValue?): T

Applies a function to produce a single value. Identical in behavior to Array.reduce().

Example

collection.reduce((acc, guild) => acc + guild.memberCount, 0);

Type parameters

Name
T

Parameters

NameTypeDescription
fn(accumulator: T, value: V, key: K, collection: Collection<K, V>) => TFunction used to reduce, taking four arguments; accumulator, currentValue, currentKey, and collection
initialValue?TStarting value for the accumulator

Returns

T

Inherited from

BaseCollection.reduce


reverse

reverse(): Collection<K, V>

Identical to Array.reverse() but returns a Collection instead of an Array.

Returns

Collection<K, V>

Inherited from

BaseCollection.reverse


set

set(key, value): Collection<K, V>

Parameters

NameType
keyK
valueV

Returns

Collection<K, V>

Inherited from

BaseCollection.set


some

some(fn): boolean

Checks if there exists an item that passes a test. Identical in behavior to Array.some().

Example

collection.some(user => user.discriminator === '0000');

Parameters

NameTypeDescription
fn(value: V, key: K, collection: Collection<K, V>) => booleanFunction used to test (should return a boolean)

Returns

boolean

Inherited from

BaseCollection.some

some<T>(fn, thisArg): boolean

Type parameters

Name
T

Parameters

NameType
fn(this: T, value: V, key: K, collection: Collection<K, V>) => boolean
thisArgT

Returns

boolean

Inherited from

BaseCollection.some


sort

sort(compareFunction?): Collection<K, V>

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);

Parameters

NameTypeDescription
compareFunction?Comparator<K, V>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

Collection<K, V>

Inherited from

BaseCollection.sort


sorted

sorted(compareFunction?): 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.

Example

collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);

Parameters

NameTypeDescription
compareFunction?Comparator<K, V>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

Collection<K, V>

Inherited from

BaseCollection.sorted


sweep

sweep(fn): number

Removes items that satisfy the provided filter function.

Parameters

NameTypeDescription
fn(value: V, key: K, collection: Collection<K, V>) => booleanFunction used to test (should return a boolean)

Returns

number

The number of removed entries

Inherited from

BaseCollection.sweep

sweep<T>(fn, thisArg): number

Type parameters

Name
T

Parameters

NameType
fn(this: T, value: V, key: K, collection: Collection<K, V>) => boolean
thisArgT

Returns

number

Inherited from

BaseCollection.sweep


tap

tap(fn): Collection<K, V>

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))

Parameters

NameTypeDescription
fn(collection: Collection<K, V>) => voidFunction to execute

Returns

Collection<K, V>

Inherited from

BaseCollection.tap

tap<T>(fn, thisArg): Collection<K, V>

Type parameters

Name
T

Parameters

NameType
fn(this: T, collection: Collection<K, V>) => void
thisArgT

Returns

Collection<K, V>

Inherited from

BaseCollection.tap


toJSON

toJSON(): any[]

Returns

any[]

Overrides

BaseCollection.toJSON


values

values(): IterableIterator<V>

Returns an iterable of values in the map

Returns

IterableIterator<V>

Inherited from

BaseCollection.values