Skip to content

Latest commit

Β 

History

History
36 lines (23 loc) Β· 1.32 KB

File metadata and controls

36 lines (23 loc) Β· 1.32 KB

prefer-array-last-methods

πŸ“ Prefer last-oriented array methods over Array#reverse() or Array#toReversed() followed by a method.

πŸ’Ό This rule is enabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

πŸ’‘ This rule is manually fixable by editor suggestions.

Prefer last-oriented array methods over reversing an array and then calling the forward method.

Last-oriented methods express the traversal direction directly and avoid reversing the array before the operation.

This rule reports .reverse() and .toReversed() followed by .find(), .findIndex(), .indexOf(), or .reduce().

This rule only provides editor suggestions. The replacement can change observable behavior for mutation, sparse arrays, callback index or array arguments, and index-returning methods.

Examples

// ❌
const result = array.reverse().find(isUnicorn);

// βœ…
const result = array.findLast(isUnicorn);
// ❌
const result = array.toReversed().reduce(reducer, initialValue);

// βœ…
const result = array.reduceRight(reducer, initialValue);