# manual-filter-map

Prefer filterMap when removing undefined values from a mapped array

Mapping an array before filtering out undefined results allocates an intermediate array.
Instead, you SHOULD call `filterMap` to keep defined mapped values in one operation.

- Category: performance
- Level: warning
- Fix: suggestion
- Scope: module

## Reported

```ds title="main.ds"
declare const values: (int32 | undefined)[];

const defined = values.map((value) => value).filter((value) => value !== undefined);
```

## Accepted

```ds title="main.ds"
declare const values: (int32 | undefined)[];

const defined = values.filterMap((value) => value);
```

## Prior art

- [Clippy · manual_filter_map](https://rust-lang.github.io/rust-clippy/master/index.html#manual_filter_map)

[language/linter/src/rules/performance/manual_filter_map.rs:8](https://github.com/destack-sh/destack/blob/main/language/linter/src/rules/performance/manual_filter_map.rs#L8)
