Collection
Represents a collection that extends the built-in Map class.
Signature
export declare class Collection<K, V> extends Map<K, V>Methods
sweep(fn: (value: V, key: K, collection: this) => unknown): number;Removes elements from the collection based on a filter function.
fn- The filter function that determines which elements to remove.
thisArg- The value to use as `this` when executing the filter function.
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: 2map<T = any>(fn: (value: V, key: K, collection: this) => T): T[];Creates a new array with the results of calling a provided function on every element in the collection.
fn- The function that produces an element of the new array.
thisArg- The value to use as `this` when executing the map function.
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']filter<S extends V>(fn: (value: V, key: K, collection: this) => value is S): S[];filter(fn: (value: V, key: K, collection: this) => boolean): V[];Creates a new array with all elements that pass the test implemented by the provided function.
fn- The function to test each element of the collection.
thisArg- The value to use as `this` when executing the filter function.
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']filterCollection<S extends V>(fn: (value: V, key: K, collection: this) => value is S): Collection<K, S>;filterCollection(fn: (value: V, key: K, collection: this) => boolean): Collection<K, V>;reduce<T = any>(fn: (accumulator: T, value: V, key: K, collection: this) => T, initialValue?: T): T;Apply a function against an accumulator and each element in the collection (from left to right) to reduce it to a single value.
fn- The function to execute on each element in the collection.
initialValue- The initial value of the accumulator.
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: 6every(fn: (value: V, key: K, collection: this) => boolean): boolean;Checks if all elements in the collection pass a test implemented by the provided function.
fn- The function to test each element of the collection.
`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: truesome(fn: (value: V, key: K, collection: this) => boolean): boolean;Checks if at least one element in the collection passes a test implemented by the provided function.
fn- The function to test each element of the collection.
`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: truefind<S extends V>(fn: (value: V, key: K, collection: this) => value is S): S | undefined;find(fn: (value: V, key: K, collection: this) => boolean): V | undefined;Returns the value of the first element in the collection that satisfies the provided testing function.
fn- The function to test each element of the collection.
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: 2findKey(fn: (value: V, key: K, collection: this) => boolean): K | undefined;Returns the first key in the collection that satisfies the provided testing function.
fn- The function to test each element of the collection.
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