Skip to main content

Function: collect()

collect<ElementType>(iterable: IterableResolvable<ElementType>): ElementType[]

Consumes the iterable and returns an array with all the elements.

Type Parameters

Type Parameter
ElementType

Parameters

ParameterTypeDescription
iterableIterableResolvable<ElementType>The iterator to convert to an array.

Returns

ElementType[]

An array containing the values of the provided iterator.

Example

import { toArray } from '@sapphire/iterator-utilities';

const array = [1, 2, 3, 4, 5];
console.log(toArray(array));
// Output: [1, 2, 3, 4, 5]

const set = new Set([1, 2, 3, 4, 5]);
console.log(toArray(set));
// Output: [1, 2, 3, 4, 5]

const map = new Map([['a', 1], ['b', 2], ['c', 3]]);
console.log(toArray(map));
// Output: [['a', 1], ['b', 2], ['c', 3]]

const string = 'hello';
console.log(toArray(string));
// Output: ['h', 'e', 'l', 'l', 'o']

Remarks

This function consumes the entire iterator.

Defined in

projects/utilities/packages/iterator-utilities/src/lib/toArray.ts:35