Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Collection<K, V>

A Map with additional utility methods.

Type parameters

  • K

  • V

Hierarchy

Index

Constructors

constructor

  • new Collection(entries?: readonly readonly [K, V][] | null): Collection

Properties

Readonly [Symbol.toStringTag]

[Symbol.toStringTag]: string

Private _array

_array: V[] | null

Private _keyArray

_keyArray: K[] | null

Readonly size

size: number
returns

the number of elements in the Map.

Static Map

Map: MapConstructor

Static Readonly default

default: typeof Collection = Collection

Methods

[Symbol.iterator]

  • [Symbol.iterator](): IterableIterator<[K, V]>
  • Returns an iterable of entries in the map.

    Returns IterableIterator<[K, V]>

array

  • array(): V[]
  • Creates an ordered array of the values of this collection, and caches it internally. The array will only be reconstructed if an item is added to or removed from the collection, or if you change the length of the array itself. If you don't want this caching behavior, use [...collection.values()] or Array.from(collection.values()) instead.

    Returns V[]

clear

  • clear(): void
  • Returns void

clone

concat

  • 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

    • Rest ...collections: Collection<K, V>[]

      Collections to merge

    Returns Collection<K, V>

delete

  • delete(key: K): boolean

each

  • each(fn: (value: V, key: K, collection: Map<K, V>) => any, thisArg?: any): this
  • 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

    • fn: (value: V, key: K, collection: Map<K, V>) => any

      Function to execute for each element

        • (value: V, key: K, collection: Map<K, V>): any
        • Parameters

          • value: V
          • key: K
          • collection: Map<K, V>

          Returns any

    • Optional thisArg: any

    Returns this

entries

  • entries(): IterableIterator<[K, V]>
  • Returns an iterable of key, value pairs for every entry in the map.

    Returns IterableIterator<[K, V]>

equals

  • Checks if this collection shares identical key-value pairings 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

    • collection: Collection<K, V>

      Collection to compare with

    Returns boolean

    Whether the collections have identical contents

every

  • every(fn: (value: V, key: K, collection: this) => boolean): boolean
  • Checks if all items passes a test. Identical in behavior to Array.every().

    example

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

    Parameters

    • fn: (value: V, key: K, collection: this) => boolean

      Function used to test (should return a boolean)

        • (value: V, key: K, collection: this): boolean
        • Parameters

          • value: V
          • key: K
          • collection: this

          Returns boolean

    Returns boolean

filter

  • filter(fn: (value: V, key: K, collection: this) => boolean): Collection<K, V>
  • Identical to Array.filter(), but returns a Collection instead of an Array.

    example

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

    Parameters

    • fn: (value: V, key: K, collection: this) => boolean

      The function to test with (should return boolean)

        • (value: V, key: K, collection: this): boolean
        • Parameters

          • value: V
          • key: K
          • collection: this

          Returns boolean

    Returns Collection<K, V>

find

  • find(fn: (value: V, key: K, collection: this) => boolean): V | undefined
  • 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');

    Parameters

    • fn: (value: V, key: K, collection: this) => boolean

      The function to test with (should return boolean)

        • (value: V, key: K, collection: this): boolean
        • Parameters

          • value: V
          • key: K
          • collection: this

          Returns boolean

    Returns V | undefined

findKey

  • findKey(fn: (value: V, key: K, collection: this) => boolean, thisArg?: any): K | undefined
  • 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');

    Parameters

    • fn: (value: V, key: K, collection: this) => boolean

      The function to test with (should return boolean)

        • (value: V, key: K, collection: this): boolean
        • Parameters

          • value: V
          • key: K
          • collection: this

          Returns boolean

    • Optional thisArg: any

    Returns K | undefined

first

  • first(): V | undefined
  • first(amount: number): V[]

firstKey

  • firstKey(): K | undefined
  • firstKey(amount: number): K[]

forEach

  • forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any): void
  • Executes a provided function once per each key/value pair in the Map, in insertion order.

    Parameters

    • callbackfn: (value: V, key: K, map: Map<K, V>) => void
        • (value: V, key: K, map: Map<K, V>): void
        • Parameters

          • value: V
          • key: K
          • map: Map<K, V>

          Returns void

    • Optional thisArg: any

    Returns void

get

  • get(key: K): V | undefined
  • 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

    • key: K

    Returns V | undefined

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

has

  • has(key: K): boolean
  • Parameters

    • key: K

    Returns boolean

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

keyArray

  • keyArray(): K[]
  • Creates an ordered array of the keys of this collection, and caches it internally. The array will only be reconstructed if an item is added to or removed from the collection, or if you change the length of the array itself. If you don't want this caching behavior, use [...collection.keys()] or Array.from(collection.keys()) instead.

    Returns K[]

keys

  • keys(): IterableIterator<K>
  • Returns an iterable of keys in the map

    Returns IterableIterator<K>

last

  • last(): V | undefined
  • last(amount: number): V[]
  • Obtains the last value(s) in this collection. This relies on {@link Collection#array}, and thus the caching mechanism applies here as well.

    Returns V | undefined

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

  • Parameters

    • amount: number

    Returns V[]

lastKey

  • lastKey(): K | undefined
  • lastKey(amount: number): K[]
  • Obtains the last key(s) in this collection. This relies on {@link Collection#keyArray}, and thus the caching mechanism applies here as well.

    Returns K | undefined

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

  • Parameters

    • amount: number

    Returns K[]

map

  • map<T>(fn: (value: V, key: K, collection: this) => T): T[]
  • Maps each item to another value into an array. Identical in behavior to Array.map().

    example

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

    Type parameters

    • T

    Parameters

    • fn: (value: V, key: K, collection: this) => T

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

        • (value: V, key: K, collection: this): T
        • Parameters

          • value: V
          • key: K
          • collection: this

          Returns T

    Returns T[]

mapValues

  • mapValues<T>(fn: (value: V, key: K, collection: this) => T): 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

    • T

    Parameters

    • fn: (value: V, key: K, collection: this) => T

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

        • (value: V, key: K, collection: this): T
        • Parameters

          • value: V
          • key: K
          • collection: this

          Returns T

    Returns Collection<K, T>

random

  • random(): V
  • random(amount: number): V[]
  • Obtains unique random value(s) from this collection. This relies on {@link Collection#array}, and thus the caching mechanism applies here as well.

    Returns V

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

  • Parameters

    • amount: number

    Returns V[]

randomKey

  • randomKey(): K
  • randomKey(amount: number): K[]
  • Obtains unique random key(s) from this collection. This relies on {@link Collection#keyArray}, and thus the caching mechanism applies here as well.

    Returns K

    A single key if no amount is provided or an array

  • Parameters

    • amount: number

    Returns K[]

reduce

  • reduce<T>(fn: (accumulator: any, value: V, key: K, collection: this) => T, initialValue?: T): 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

    • T

    Parameters

    • fn: (accumulator: any, value: V, key: K, collection: this) => T

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

        • (accumulator: any, value: V, key: K, collection: this): T
        • Parameters

          • accumulator: any
          • value: V
          • key: K
          • collection: this

          Returns T

    • Optional initialValue: T

    Returns T

set

  • set(key: K, value: V): this

some

  • some(fn: (value: V, key: K, collection: this) => boolean): 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

    • fn: (value: V, key: K, collection: this) => boolean

      Function used to test (should return a boolean)

        • (value: V, key: K, collection: this): boolean
        • Parameters

          • value: V
          • key: K
          • collection: this

          Returns boolean

    Returns boolean

sort

  • sort(compareFunction?: (firstValue: V, secondValue: V, firstKey: K, secondKey: K) => number): this
  • The sort() method sorts the elements of a collection and returns it. The sort is not necessarily stable. The default sort order is according to string Unicode code points.

    example

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

    Parameters

    • Default value compareFunction: (firstValue: V, secondValue: V, firstKey: K, secondKey: K) => number = (x, y): number =>Number(x > y) || Number(x === y) - 1
        • (firstValue: V, secondValue: V, firstKey: K, secondKey: K): number
        • Parameters

          • firstValue: V
          • secondValue: V
          • firstKey: K
          • secondKey: K

          Returns number

    Returns this

sweep

  • sweep(fn: (value: V, key: K, collection: this) => boolean): number
  • Removes entries that satisfy the provided filter function.

    Parameters

    • fn: (value: V, key: K, collection: this) => boolean

      Function used to test (should return a boolean)

        • (value: V, key: K, collection: this): boolean
        • Parameters

          • value: V
          • key: K
          • collection: this

          Returns boolean

    Returns number

    The number of removed entries

tap

  • tap(fn: (collection: this) => any): this
  • 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

    • fn: (collection: this) => any

      Function to execute

        • (collection: this): any
        • Parameters

          • collection: this

          Returns any

    Returns this

values

  • values(): IterableIterator<V>
  • Returns an iterable of values in the map

    Returns IterableIterator<V>