✅ 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()
withundefined
.
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.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>
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>