💼 This rule is enabled in the ✅ recommended
config.
🔧 This rule is automatically fixable by the --fix
CLI option.
ES2019 introduced a new method Array#flat()
that flatten arrays.
const foo = array.flatMap(x => x);
const foo = array.reduce((a, b) => a.concat(b), []);
const foo = array.reduce((a, b) => [...a, ...b], []);
const foo = [].concat(maybeArray);
const foo = [].concat(...array);
const foo = [].concat.apply([], array);
const foo = Array.prototype.concat.apply([], array);
const foo = Array.prototype.concat.call([], maybeArray);
const foo = Array.prototype.concat.call([], ...array);
const foo = _.flatten(array);
const foo = lodash.flatten(array);
const foo = underscore.flatten(array);
const foo = array.flat();
const foo = [maybeArray].flat();
Type: object
Type: string[]
You can also check custom functions that flatten arrays.
_.flatten()
, lodash.flatten()
, and underscore.flatten()
are always checked.
Example:
{
'unicorn/prefer-array-flat': [
'error',
{
functions: [
'flatArray',
'utils.flat'
]
}
]
}
// eslint unicorn/prefer-array-flat: ["error", {"functions": ["utils.flat"]}]
const foo = utils.flat(bar); // Fails