Skip to content

Files

Latest commit

 

History

History
24 lines (18 loc) · 623 Bytes

prefer-array-flat.md

File metadata and controls

24 lines (18 loc) · 623 Bytes

Pattern: Legacy array flattening technique

Issue: -

Description

ES2019's Array#flat() method provides a cleaner way to flatten arrays compared to older techniques like reduce(), concat(), or spread operators.

Examples

Example of incorrect code:

const foo = array.reduce((a, b) => a.concat(b), []);
const foo = array.reduce((a, b) => [...a, ...b], []);
const foo = [].concat(...array);
const foo = [].concat.apply([], array);
const foo = Array.prototype.concat.apply([], array);

Example of correct code:

const foo = array.flat();
const foo = [maybeArray].flat();