Skip to content

Latest commit

 

History

History
49 lines (35 loc) · 1.25 KB

prefer-array-some.md

File metadata and controls

49 lines (35 loc) · 1.25 KB

Prefer .some(…) over .filter(…).length check and .find(…)

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(…).

Fail

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;

Pass

const hasUnicorn = array.some(element => isUnicorn(element));
if (array.some(element => isUnicorn(element))) {
	// …
}
const foo = array.find(element => isUnicorn(element)) || bar;