Skip to main content

Function: toArray()

toArray<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.

Source

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