API reference › @evolu/common › Array › flatMapArray
Call Signature
function flatMapArray<T>(
array:
| readonly [readonly [T, T] | [T, ...T[]], readonly [T, T] | [T, ...T[]]]
| [readonly [T, T] | [T, ...T[]], ...(readonly [T, T] | [T, ...T[]])[]],
): readonly [T, T];
Defined in: packages/common/src/Array.ts:363
Maps each element to an array and flattens the result.
Preserves non-empty type when the input is non-empty and the mapper returns non-empty arrays. When called without a mapper, flattens nested arrays using identity.
Example
flatMapArray([
[1, 2],
[3, 4],
]); // [1, 2, 3, 4]
flatMapArray([1, 2, 3], (x) => [x, x * 10]); // [1, 10, 2, 20, 3, 30]
Filter and map in one pass
Return [] to filter out, [value] to keep:
const errors = flatMapArray(fields, (f) => {
const result = validate(f);
return result.ok ? [] : [result.error];
});
Call Signature
function flatMapArray<T>(
array: readonly (readonly T[] | T[])[] | (readonly T[] | T[])[],
): readonly T[];
Defined in: packages/common/src/Array.ts:369
Possibly empty nested arrays.
Call Signature
function flatMapArray<T, U>(
array: readonly [T, T] | [T, ...T[]],
mapper: (item: T, index: number) => readonly [U, U] | [U, ...U[]],
): readonly [U, U];
Defined in: packages/common/src/Array.ts:375
Non-empty with mapper returning non-empty.
Call Signature
function flatMapArray<T, U>(
array: readonly T[] | T[],
mapper: (item: T, index: number) => readonly U[] | U[],
): readonly U[];
Defined in: packages/common/src/Array.ts:383
With mapper function.