Skip to content

Latest commit

 

History

History
80 lines (59 loc) · 2.21 KB

prefer-array-some.md

File metadata and controls

80 lines (59 loc) · 2.21 KB

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

This rule is part of the recommended config.

🔧💡 This rule is auto-fixable and provides suggestions.

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.

  • Comparing the result of Array#find() with undefined.

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;
const hasUnicorn = array.find(element => isUnicorn(element) !== undefined;
const hasUnicorn = array.find(element => isUnicorn(element) != null;
<template>
	<div v-if="array.find(element => isUnicorn(element))">Vue</div>
</template>
<template>
	<div v-if="array.filter(element => isUnicorn(element)).length > 0">Vue</div>
</template>

Pass

const hasUnicorn = array.some(element => isUnicorn(element));
if (array.some(element => isUnicorn(element))) {
	// …
}
const foo = array.find(element => isUnicorn(element)) || bar;
<template>
	<div v-if="array.some(element => isUnicorn(element))">Vue</div>
</template>