Skip to content

toArray() function

Iterable/AsyncIterable から項目を取得し、配列を返します。pipeと一緒に使用することをお勧めします。

Signature:

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

Example

ts
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

Released under the Apache-2.0 License.