Example 1 — Map Each Item to Several Values
Return an array from the callback; those items join the result.
const numbers = [1, 2, 3];
const expanded = numbers.flatMap((n) => [n, n * 2]);
console.log(expanded);
// [1, 2, 2, 4, 3, 6] How It Works
- Callback per item. Each number returns
[n, n * 2]. - One-level flatten. Those small arrays are unwrapped into one list.
- Order preserved. Results appear in source order.
- New array.
numbersis unchanged.

