Skip to main content

flat

flat() function

Returns flattened Iterable/AsyncIterable. If first argument is number, more perform flatten flat([[[1,2]]], 2) // [1,2]

Signature:
declare function flat<A extends Iterable<unknown> | AsyncIterable<unknown>, B extends number = 1>(iterator: A, depth?: B): ReturnFlatType<A, B>;

Example

const iter = flat([1,[2,3],[[4,5]]]);
iter.next() // {done:false, value: 1}
iter.next() // {done:false, value: 2}
iter.next() // {done:false, value: 3}
iter.next() // {done:false, value: [4, 5]}
iter.next() // {done:true, value: undefined}

// with pipe
pipe(
[1,[2, 3],[[4, 5]]],
flat,
toArray,
); // [1, 2, 3, [4, 5]]

await pipe(
Promise.resolve([1,[2, 3],[[4, 5]]]),
flat,
toArray,
); // [1, 2, 3, [4, 5]]

Try It

see pipe, toAsync, toArray

Open Source Code