Skip to main content

toArray

toArray() function

Takes item from Iterable/AsyncIterable and returns an array. It is recommended to use pipe together.

Signature:
declare function toArray<A extends Iterable<unknown> | AsyncIterable<unknown>>(iter: A): ReturnArrayType<A>;

Example

pipe(
[1, 2, 3, 4, 5],
map(a => a + 10),
filter(a => a % 2 === 0),
toArray,
); // [12, 14]

await pipe(
Promise.resolve([1, 2, 3, 4, 5]),
map(a => a + 10),
filter(a => a % 2 === 0),
toArray,
); // [12, 14]

// if you want to use asynchronous callback
await pipe(
Promise.resolve([1, 2, 3, 4, 5]),
toAsync,
map(async (a) => a + 10),
filter(a => a % 2 === 0),
toArray,
); // [12, 14]

// with toAsync
await pipe(
[
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3),
Promise.resolve(4),
Promise.resolve(5)
],
toAsync,
map((a) => a + 10),
filter((a) => a % 2 === 0),
toArray
);

Try It

see pipe, toAsync, map, filter

Open Source Code