Prefer using Array#some
over:
- Non-zero length check on the result of
Array#filter()
. - Using
Array#find()
to ensure at least one element in the array passes a given check.
This rule is fixable for .filter(…).length
check and has a suggestion for .find(…)
.
const hasUnicorn = array.filter(element => isUnicorn(element)).length > 0;
const hasUnicorn = array.filter(element => isUnicorn(element)).length != 0;
const hasUnicorn = array.filter(element => isUnicorn(element)).length >= 1;
if (array.find(element => isUnicorn(element))) {
// …
}
const foo = array.find(element => isUnicorn(element)) ? bar : baz;
const hasUnicorn = array.some(element => isUnicorn(element));
if (array.some(element => isUnicorn(element))) {
// …
}
const foo = array.find(element => isUnicorn(element)) || bar;