Skip to content

Files

Latest commit

 

History

History
19 lines (13 loc) · 389 Bytes

prefer-array-flat-map.md

File metadata and controls

19 lines (13 loc) · 389 Bytes

Pattern: Chained map().flat() calls

Issue: -

Description

Using flatMap() is more efficient than chaining map() and flat() methods, as it avoids creating an intermediate array.

Examples

Example of incorrect code:

const bar = [1, 2, 3].map((i) => [i]).flat();

Example of correct code:

const bar = [1, 2, 3].flatMap((i) => [i]);