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

  • Key

    The key type this collection holds

  • Value

    The value type this collection holds

Hierarchy (View Summary)

Constructors

Properties

"[toStringTag]": string

The initial value of Object.prototype.constructor is the standard built-in Object constructor.

keepOverLimit: null | (value: Value, key: Key, collection: this) => boolean
maxSize: number
size: number

the number of elements in the Map.

"[species]": MapConstructor

Methods

  • 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

    • index: number

      The index of the element to obtain

    Returns undefined | Value

  • Returns void

  • Creates an identical shallow copy of this collection.

    Returns Collection<Key, Value>

    const newColl = someColl.clone();
    
  • Parameters

    Returns boolean

    true if an element in the Map existed and has been removed, or false if the element does not exist.

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

    Parameters

    • fn: (value: Value, key: Key, collection: this) => void

      Function to execute for each element

    Returns this

    collection
    .each(user => console.log(user.username))
    .filter(user => user.bot)
    .each(user => console.log(user.username));
  • Identical to Map.forEach(), but returns the collection instead of undefined.

    Type Parameters

    • T

    Parameters

    • fn: (this: T, value: Value, key: Key, collection: this) => void

      Function to execute for each element

    • thisArg: T

      Value to use as this when executing function

    Returns this

    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.

    Parameters

    • key: Key

      The key to get if it exists, or set otherwise

    • defaultValueGenerator: (key: Key, collection: this) => Value

      A function that generates the default value

    Returns 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.

    Parameters

    Returns boolean

    Whether the collections have identical contents

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

    Type Parameters

    • K2

    Parameters

    • fn: (value: Value, key: Key, collection: this) => key is K2

      Function used to test (should return a boolean)

    Returns this is Collection<K2, Value>

    collection.every(user => !user.bot);
    
  • Checks if all items passes a test. Identical in behavior to Array.every().

    Type Parameters

    • V2

    Parameters

    • fn: (value: Value, key: Key, collection: this) => value is V2

      Function used to test (should return a boolean)

    Returns this is Collection<Key, V2>

    collection.every(user => !user.bot);
    
  • Checks if all items passes a test. Identical in behavior to Array.every().

    Parameters

    • fn: (value: Value, key: Key, collection: this) => unknown

      Function used to test (should return a boolean)

    Returns boolean

    collection.every(user => !user.bot);
    
  • Checks if all items passes a test. Identical in behavior to Array.every().

    Type Parameters

    • This
    • K2

    Parameters

    • fn: (this: This, value: Value, key: Key, collection: this) => key is K2

      Function used to test (should return a boolean)

    • thisArg: This

      Value to use as this when executing function

    Returns this is Collection<K2, Value>

    collection.every(user => !user.bot);
    
  • Checks if all items passes a test. Identical in behavior to Array.every().

    Type Parameters

    • This
    • V2

    Parameters

    • fn: (this: This, value: Value, key: Key, collection: this) => value is V2

      Function used to test (should return a boolean)

    • thisArg: This

      Value to use as this when executing function

    Returns this is Collection<Key, V2>

    collection.every(user => !user.bot);
    
  • Checks if all items passes a test. Identical in behavior to Array.every().

    Type Parameters

    • This

    Parameters

    • fn: (this: This, value: Value, key: Key, collection: this) => unknown

      Function used to test (should return a boolean)

    • thisArg: This

      Value to use as this when executing function

    Returns boolean

    collection.every(user => !user.bot);
    
  • Identical to Array.filter(), but returns a Collection instead of an Array.

    Type Parameters

    • K2

    Parameters

    • fn: (value: Value, key: Key, collection: this) => key is K2

      The function to test with (should return boolean)

    Returns Collection<K2, Value>

    collection.filter(user => user.username === 'Bob');
    
  • Identical to Array.filter(), but returns a Collection instead of an Array.

    Type Parameters

    • V2

    Parameters

    • fn: (value: Value, key: Key, collection: this) => value is V2

      The function to test with (should return boolean)

    Returns Collection<Key, V2>

    collection.filter(user => user.username === 'Bob');
    
  • Identical to Array.filter(), but returns a Collection instead of an Array.

    Parameters

    • fn: (value: Value, key: Key, collection: this) => unknown

      The function to test with (should return boolean)

    Returns Collection<Key, Value>

    collection.filter(user => user.username === 'Bob');
    
  • Identical to Array.filter(), but returns a Collection instead of an Array.

    Type Parameters

    • This
    • K2

    Parameters

    • fn: (this: This, value: Value, key: Key, collection: this) => key is K2

      The function to test with (should return boolean)

    • thisArg: This

      Value to use as this when executing function

    Returns Collection<K2, Value>

    collection.filter(user => user.username === 'Bob');
    
  • Identical to Array.filter(), but returns a Collection instead of an Array.

    Type Parameters

    • This
    • V2

    Parameters

    • fn: (this: This, value: Value, key: Key, collection: this) => value is V2

      The function to test with (should return boolean)

    • thisArg: This

      Value to use as this when executing function

    Returns Collection<Key, V2>

    collection.filter(user => user.username === 'Bob');
    
  • Identical to Array.filter(), but returns a Collection instead of an Array.

    Type Parameters

    • This

    Parameters

    • fn: (this: This, value: Value, key: Key, collection: this) => unknown

      The function to test with (should return boolean)

    • thisArg: This

      Value to use as this when executing function

    Returns Collection<Key, Value>

    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.

    Type Parameters

    • V2

    Parameters

    • fn: (value: Value, key: Key, collection: this) => value is V2

      The function to test with (should return boolean)

    Returns undefined | V2

    collection.find(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.

    Parameters

    • fn: (value: Value, key: Key, collection: this) => unknown

      The function to test with (should return boolean)

    Returns undefined | Value

    collection.find(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.

    Type Parameters

    • This
    • V2

    Parameters

    • fn: (this: This, value: Value, key: Key, collection: this) => value is V2

      The function to test with (should return boolean)

    • thisArg: This

      Value to use as this when executing function

    Returns undefined | V2

    collection.find(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.

    Type Parameters

    • This

    Parameters

    • fn: (this: This, value: Value, key: Key, collection: this) => unknown

      The function to test with (should return boolean)

    • thisArg: This

      Value to use as this when executing function

    Returns undefined | Value

    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.

    Type Parameters

    • K2

    Parameters

    • fn: (value: Value, key: Key, collection: this) => key is K2

      The function to test with (should return boolean)

    Returns undefined | K2

    collection.findKey(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.

    Parameters

    • fn: (value: Value, key: Key, collection: this) => unknown

      The function to test with (should return boolean)

    Returns undefined | Key

    collection.findKey(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.

    Type Parameters

    • This
    • K2

    Parameters

    • fn: (this: This, value: Value, key: Key, collection: this) => key is K2

      The function to test with (should return boolean)

    • thisArg: This

      Value to use as this when executing function

    Returns undefined | K2

    collection.findKey(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.

    Type Parameters

    • This

    Parameters

    • fn: (this: This, value: Value, key: Key, collection: this) => unknown

      The function to test with (should return boolean)

    • thisArg: This

      Value to use as this when executing function

    Returns undefined | Key

    collection.findKey(user => user.username === 'Bob');
    
  • Obtains the first value(s) in this collection.

    Returns undefined | Value

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

  • Obtains the first value(s) in this collection.

    Parameters

    • amount: number

      Amount of values to obtain from the beginning

    Returns Value[]

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

  • Obtains the first key(s) in this collection.

    Returns undefined | Key

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

  • Obtains the first key(s) in this collection.

    Parameters

    • amount: number

      Amount of keys to obtain from the beginning

    Returns Key[]

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

  • Maps each item into a Collection, then joins the results into a single Collection. Identical in behavior to Array.flatMap().

    Type Parameters

    • T

    Parameters

    Returns Collection<Key, T>

    collection.flatMap(guild => guild.members.cache);
    
  • Maps each item into a Collection, then joins the results into a single Collection. Identical in behavior to Array.flatMap().

    Type Parameters

    • T
    • This

    Parameters

    • fn: (this: This, value: Value, key: Key, collection: this) => Collection<Key, T>

      Function that produces a new Collection

    • thisArg: This

      Value to use as this when executing function

    Returns Collection<Key, T>

    collection.flatMap(guild => guild.members.cache);
    
  • Executes a provided function once per each key/value pair in the Map, in insertion order.

    Parameters

    Returns void

  • Returns 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.

    Parameters

    Returns undefined | Value

    Returns the element associated with the specified key. If no element is associated with the specified key, undefined is returned.

  • Parameters

    Returns boolean

    boolean indicating whether an element with the specified key exists or not.

  • Checks if all of the elements exist in the collection.

    Parameters

    • ...keys: Key[]

      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.

  • Checks if any of the elements exist in the collection.

    Parameters

    • ...keys: Key[]

      The keys of the elements to check for

    Returns boolean

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

  • 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

    • index: number

      The index of the key to obtain

    Returns undefined | Key

  • Obtains the last value(s) in this collection.

    Returns undefined | Value

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

  • Obtains the last value(s) in this collection.

    Parameters

    • amount: number

      Amount of values to obtain from the end

    Returns Value[]

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

  • Obtains the last key(s) in this collection.

    Returns undefined | Key

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

  • Obtains the last key(s) in this collection.

    Parameters

    • amount: number

      Amount of keys to obtain from the end

    Returns Key[]

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

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

    Type Parameters

    • T

    Parameters

    • fn: (value: Value, key: Key, collection: this) => T

      Function that produces an element of the new array, taking three arguments

    Returns T[]

    collection.map(user => user.tag);
    
  • Maps each item to another value into an array. Identical in behavior to Array.map().

    Type Parameters

    • This
    • T

    Parameters

    • fn: (this: This, value: Value, key: Key, collection: this) => T

      Function that produces an element of the new array, taking three arguments

    • thisArg: This

      Value to use as this when executing function

    Returns T[]

    collection.map(user => user.tag);
    
  • Maps each item to another value into a collection. Identical in behavior to Array.map().

    Type Parameters

    • T

    Parameters

    • fn: (value: Value, key: Key, collection: this) => T

      Function that produces an element of the new collection, taking three arguments

    Returns Collection<Key, T>

    collection.mapValues(user => user.tag);
    
  • Maps each item to another value into a collection. Identical in behavior to Array.map().

    Type Parameters

    • This
    • T

    Parameters

    • fn: (this: This, value: Value, key: Key, collection: this) => T

      Function that produces an element of the new collection, taking three arguments

    • thisArg: This

      Value to use as this when executing function

    Returns Collection<Key, T>

    collection.mapValues(user => user.tag);
    
  • Merges two Collections together into a new Collection.

    Type Parameters

    • T
    • R

    Parameters

    • other: ReadonlyCollection<Key, T>

      The other Collection to merge with

    • whenInSelf: (value: Value, key: Key) => Keep<R>

      Function getting the result if the entry only exists in this Collection

    • whenInOther: (valueOther: T, key: Key) => Keep<R>

      Function getting the result if the entry only exists in the other Collection

    • whenInBoth: (value: Value, valueOther: T, key: Key) => Keep<R>

      Function getting the result if the entry exists in both Collections

    Returns Collection<Key, R>

    // 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.

    Type Parameters

    • K2

    Parameters

    • fn: (value: Value, key: Key, collection: this) => key is K2

      Function used to test (should return a boolean)

    Returns [Collection<K2, Value>, Collection<Exclude<Key, K2>, Value>]

    const [big, small] = collection.partition(guild => guild.memberCount > 250);
    
  • Partitions the collection into two collections where the first collection contains the items that passed and the second contains the items that failed.

    Type Parameters

    • V2

    Parameters

    • fn: (value: Value, key: Key, collection: this) => value is V2

      Function used to test (should return a boolean)

    Returns [Collection<Key, V2>, Collection<Key, Exclude<Value, V2>>]

    const [big, small] = collection.partition(guild => guild.memberCount > 250);
    
  • Partitions the collection into two collections where the first collection contains the items that passed and the second contains the items that failed.

    Parameters

    • fn: (value: Value, key: Key, collection: this) => unknown

      Function used to test (should return a boolean)

    Returns [Collection<Key, Value>, Collection<Key, Value>]

    const [big, small] = collection.partition(guild => guild.memberCount > 250);
    
  • Partitions the collection into two collections where the first collection contains the items that passed and the second contains the items that failed.

    Type Parameters

    • This
    • K2

    Parameters

    • fn: (this: This, value: Value, key: Key, collection: this) => key is K2

      Function used to test (should return a boolean)

    • thisArg: This

      Value to use as this when executing function

    Returns [Collection<K2, Value>, Collection<Exclude<Key, K2>, Value>]

    const [big, small] = collection.partition(guild => guild.memberCount > 250);
    
  • Partitions the collection into two collections where the first collection contains the items that passed and the second contains the items that failed.

    Type Parameters

    • This
    • V2

    Parameters

    • fn: (this: This, value: Value, key: Key, collection: this) => value is V2

      Function used to test (should return a boolean)

    • thisArg: This

      Value to use as this when executing function

    Returns [Collection<Key, V2>, Collection<Key, Exclude<Value, V2>>]

    const [big, small] = collection.partition(guild => guild.memberCount > 250);
    
  • Partitions the collection into two collections where the first collection contains the items that passed and the second contains the items that failed.

    Type Parameters

    • This

    Parameters

    • fn: (this: This, value: Value, key: Key, collection: this) => unknown

      Function used to test (should return a boolean)

    • thisArg: This

      Value to use as this when executing function

    Returns [Collection<Key, Value>, Collection<Key, Value>]

    const [big, small] = collection.partition(guild => guild.memberCount > 250);
    
  • Obtains unique random value(s) from this collection.

    Returns undefined | Value

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

  • Obtains unique random value(s) from this collection.

    Parameters

    • amount: number

      Amount of values to obtain randomly

    Returns Value[]

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

  • Obtains unique random key(s) from this collection.

    Returns undefined | Key

    A single key if no amount is provided or an array

  • Obtains unique random key(s) from this collection.

    Parameters

    • amount: number

      Amount of keys to obtain randomly

    Returns Key[]

    A single key if no amount is provided or an array

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

    Type Parameters

    Parameters

    • fn: (accumulator: T, value: Value, key: Key, collection: this) => T

      Function used to reduce, taking four arguments; accumulator, currentValue, currentKey, and collection

    • OptionalinitialValue: T

      Starting value for the accumulator

    Returns T

    collection.reduce((acc, guild) => acc + guild.memberCount, 0);
    
  • Identical to Array.reverse() but returns a Collection instead of an Array.

    Returns this

  • 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.

    Parameters

    Returns this

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

    Parameters

    • fn: (value: Value, key: Key, collection: this) => unknown

      Function used to test (should return a boolean)

    Returns boolean

    collection.some(user => user.discriminator === '0000');
    
  • Checks if there exists an item that passes a test. Identical in behavior to Array.some().

    Type Parameters

    • T

    Parameters

    • fn: (this: T, value: Value, key: Key, collection: this) => unknown

      Function used to test (should return a boolean)

    • thisArg: T

      Value to use as this when executing function

    Returns 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.

    Parameters

    • OptionalcompareFunction: 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.

    Returns this

    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.

    Parameters

    • OptionalcompareFunction: 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.

    Returns Collection<Key, Value>

    collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
    
  • Removes items that satisfy the provided filter function.

    Parameters

    • fn: (value: Value, key: Key, collection: this) => unknown

      Function used to test (should return a boolean)

    Returns number

    The number of removed entries

  • Removes items that satisfy the provided filter function.

    Type Parameters

    • T

    Parameters

    • fn: (this: T, value: Value, key: Key, collection: this) => unknown

      Function used to test (should return a boolean)

    • thisArg: T

      Value to use as this when executing function

    Returns number

    The number of removed entries

  • Runs a function on the collection and returns the collection.

    Parameters

    • fn: (collection: this) => void

      Function to execute

    Returns this

    collection
    .tap(coll => console.log(coll.size))
    .filter(user => user.bot)
    .tap(coll => console.log(coll.size))
  • Runs a function on the collection and returns the collection.

    Type Parameters

    • T

    Parameters

    • fn: (this: T, collection: this) => void

      Function to execute

    • thisArg: T

      Value to use as this when executing function

    Returns this

    collection
    .tap(coll => console.log(coll.size))
    .filter(user => user.bot)
    .tap(coll => console.log(coll.size))
  • Creates a Collection from a list of entries.

    Type Parameters

    • K
    • V

    Parameters

    • entries: Iterable<[K, V]>

      The list of entries

    • combine: (firstValue: V, secondValue: V, key: K) => V

      Function to combine an existing entry with a new one

    Returns Collection<K, V>

    Collection.combineEntries([["a", 1], ["b", 2], ["a", 2]], (x, y) => x + y);
    // returns Collection { "a" => 3, "b" => 2 }
  • Groups members of an iterable according to the return value of the passed callback.

    Type Parameters

    • K
    • T

    Parameters

    • items: Iterable<T>

      An iterable.

    • keySelector: (item: T, index: number) => K

      A callback which will be invoked for each item in items.

    Returns Map<K, T[]>