seyfert
    Preparing search index...

    Class Collection<K, V>

    Represents a collection that extends the built-in Map class.

    Type Parameters

    • K

      The type of the keys in the collection.

    • V

      The type of the values in the collection.

    Hierarchy

    • Map<K, V>
      • Collection
    Index

    Constructors

    • Type Parameters

      • K
      • V

      Parameters

      • Optionalentries: null | readonly (readonly [K, V])[]

      Returns Collection<K, V>

    • Type Parameters

      • K
      • V

      Parameters

      • Optionaliterable: null | Iterable<readonly [K, V], any, any>

      Returns Collection<K, V>

    Properties

    "[toStringTag]": string
    size: number

    the number of elements in the Map.

    "[species]": MapConstructor

    Methods

    • Returns an iterable of entries in the map.

      Returns MapIterator<[K, V]>

    • Returns void

    • Parameters

      • key: K

      Returns boolean

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

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

      Returns MapIterator<[K, V]>

    • Checks if all elements in the collection pass a test implemented by the provided function.

      Parameters

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

        The function to test each element of the collection.

      Returns boolean

      true if all elements pass the test, otherwise false.

      const collection = new Collection<number, number>();
      collection.set(1, 1);
      collection.set(2, 2);
      collection.set(3, 3);
      const allGreaterThanZero = collection.every(value => value > 0);
      console.log(allGreaterThanZero); // Output: true
    • Creates a new array with all elements that pass the test implemented by the provided function.

      Parameters

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

        The function to test each element of the collection.

      Returns V[]

      A new array with the elements that pass the test.

      const collection = new Collection<number, string>();
      collection.set(1, 'one');
      collection.set(2, 'two');
      collection.set(3, 'three');
      const filteredArray = collection.filter((value, key) => key % 2 === 0);
      console.log(filteredArray); // Output: ['two']
    • Returns the value of the first element in the collection that satisfies the provided testing function.

      Parameters

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

        The function to test each element of the collection.

      Returns undefined | V

      The value of the first element that passes the test. undefined if no element passes the test.

      const collection = new Collection<number, number>();
      collection.set(1, 1);
      collection.set(2, 2);
      collection.set(3, 3);
      const firstEvenValue = collection.find(value => value % 2 === 0);
      console.log(firstEvenValue); // Output: 2
    • Returns the first key in the collection that satisfies the provided testing function.

      Parameters

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

        The function to test each element of the collection.

      Returns undefined | K

      The first key that passes the test. undefined if no element passes the test.

      const collection = new Collection<number, number>();
      collection.set(1, 1);
      collection.set(2, 2);
      collection.set(3, 3);
      const firstEvenKey = collection.findKey(value => value % 2 === 0);
      console.log(firstEvenKey); // Output: 2
    • 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
      • OptionalthisArg: any

      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

      • key: K

      Returns undefined | V

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

    • Parameters

      • key: K

      Returns boolean

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

    • Returns an iterable of keys in the map

      Returns MapIterator<K>

    • Creates a new array with the results of calling a provided function on every element in the collection.

      Type Parameters

      • T = any

      Parameters

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

        The function that produces an element of the new array.

      Returns T[]

      A new array with the results of calling the provided function on every element in the collection.

      const collection = new Collection<number, string>();
      collection.set(1, 'one');
      collection.set(2, 'two');
      collection.set(3, 'three');
      const mappedArray = collection.map((value, key) => `${key}: ${value}`);
      console.log(mappedArray); // Output: ['1: one', '2: two', '3: three']
    • Apply a function against an accumulator and each element in the collection (from left to right) to reduce it to a single value.

      Type Parameters

      • T = any

      Parameters

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

        The function to execute on each element in the collection.

      • OptionalinitialValue: T

        The initial value of the accumulator.

      Returns T

      The value that results from the reduction.

      const collection = new Collection<number, number>();
      collection.set(1, 1);
      collection.set(2, 2);
      collection.set(3, 3);
      const sum = collection.reduce((acc, value) => acc + value, 0);
      console.log(sum); // Output: 6
    • 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

      • key: K
      • value: V

      Returns this

    • Checks if at least one element in the collection passes a test implemented by the provided function.

      Parameters

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

        The function to test each element of the collection.

      Returns boolean

      true if at least one element passes the test, otherwise false.

      const collection = new Collection<number, number>();
      collection.set(1, 1);
      collection.set(2, 2);
      collection.set(3, 3);
      const hasEvenValue = collection.some(value => value % 2 === 0);
      console.log(hasEvenValue); // Output: true
    • Removes elements from the collection based on a filter function.

      Parameters

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

        The filter function that determines which elements to remove.

      Returns number

      The number of elements removed from the collection.

      const collection = new Collection<number, string>();
      collection.set(1, 'one');
      collection.set(2, 'two');
      collection.set(3, 'three');
      const removedCount = collection.sweep((value, key) => key % 2 === 0);
      console.log(removedCount); // Output: 1
      console.log(collection.size); // Output: 2
    • Returns an iterable of values in the map

      Returns MapIterator<V>

    • 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[]>