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: readonly (readonly [K, V])[] | null

      Returns Collection<K, V>

    • Type Parameters

      • K
      • V

      Parameters

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

      Returns Collection<K, V>

    Methods

    • 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 V | undefined

      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 K | undefined

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