Skip to content

flat

The flat() function flattens nested iterables into a single generator. By default it expands one level, with configurable depth.

Interactive example

Syntax

typescript
function flat<
	const GenericValue extends unknown,
	const GenericDepth extends number = 1,
>(
	iterator: Iterable<GenericValue>,
	depth?: GenericDepth,
): Generator<FlatIterator<GenericValue, GenericDepth>, void, unknown>

Parameters

  • iterator: Iterable to flatten.
  • depth: Number of levels to expand. Defaults to 1.

Return value

A lazy Generator emitting the flattened values. Non-iterable values are yielded as-is.

See also

  • asyncFlat - Async flattening version
  • map - Transforms the elements of a generator
  • chunk - Groups elements into blocks

Released under the MIT license.