close

prefer-array-flat

Configuration

rslint.config.ts
import { defineConfig, unicornPlugin } from '@rslint/core';

export default defineConfig([
  unicornPlugin.configs.recommended,
  {
    rules: {
      'unicorn/prefer-array-flat': 'error',
    },
  },
]);

Rule Details

Prefer Array#flat() over legacy techniques that flatten arrays with identity flatMap callbacks, reduce, concat, Lodash, Underscore, or configured helper functions.

Examples of incorrect code for this rule:

const first = array.flatMap(element => element);
const second = array.reduce((result, element) => result.concat(element), []);
const third = array.reduce((result, element) => [...result, ...element], []);
const fourth = [].concat(...array);
const fifth = [].concat.apply([], array);
const sixth = Array.prototype.concat.call([], ...array);
const seventh = _.flatten(array);
const eighth = lodash.flatten(array);
const ninth = underscore.flatten(array);

Examples of correct code for this rule:

const first = array.flat();
const second = [maybeArray].flat();

[maybeArray].flat() preserves the behavior of [].concat(maybeArray) when the value may be either a single item or an array.

Options

functions

Type: string[]
Default: []

Adds custom flattening functions. _.flatten(), lodash.flatten(), and underscore.flatten() are always checked.

{
  "unicorn/prefer-array-flat": [
    "error",
    { "functions": ["flatArray", "utils.flat"] }
  ]
}

Examples of incorrect code for this rule with this option:

const first = flatArray(array);
const second = utils.flat(array);

Original Documentation