Skip to content

Latest commit

 

History

History
33 lines (23 loc) · 694 Bytes

prefer-array-find.md

File metadata and controls

33 lines (23 loc) · 694 Bytes

Prefer .find(…) over the first element from .filter(…)

Array#find() breaks the loop as soon as it finds a match and doesn't create a new array.

This rule is fixable unless default values are used in declaration or assignment.

Fail

const item = array.filter(x => isUnicorn(x))[0];
const item = array.filter(x => isUnicorn(x)).shift();
const [item] = array.filter(x => isUnicorn(x));
[item] = array.filter(x => isUnicorn(x));

Pass

const item = array.find(x => isUnicorn(x));
item = array.find(x => isUnicorn(x));