[API reference](https://evolu.dev/docs/api-reference) › [@evolu/common](https://evolu.dev/docs/api-reference/common) › [Array](https://evolu.dev/docs/api-reference/common/Array) › flatMapArray

## Call Signature

```ts
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](https://github.com/evoluhq/evolu/blob/e7144e2bbe9069362b62dec1b64a8aa922b8f1b0/packages/common/src/Array.ts#L363)

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](https://evolu.dev/docs/api-reference/common/Function/functions/identity).

### Example

```ts
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:

```ts
const errors = flatMapArray(fields, (f) => {
  const result = validate(f);
  return result.ok ? [] : [result.error];
});
```

## Call Signature

```ts
function flatMapArray<T>(
  array: readonly (readonly T[] | T[])[] | (readonly T[] | T[])[],
): readonly T[];
```

Defined in: [packages/common/src/Array.ts:369](https://github.com/evoluhq/evolu/blob/e7144e2bbe9069362b62dec1b64a8aa922b8f1b0/packages/common/src/Array.ts#L369)

Possibly empty nested arrays.

## Call Signature

```ts
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](https://github.com/evoluhq/evolu/blob/e7144e2bbe9069362b62dec1b64a8aa922b8f1b0/packages/common/src/Array.ts#L375)

Non-empty with mapper returning non-empty.

## Call Signature

```ts
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](https://github.com/evoluhq/evolu/blob/e7144e2bbe9069362b62dec1b64a8aa922b8f1b0/packages/common/src/Array.ts#L383)

With mapper function.