Skip to content

arrayFinds

Reports using .filter()[0] instead of .find() when looking for a single element.

✅ This rule is included in the ts stylistic and stylisticStrict presets.

When searching for the first item in an array matching a condition, using .filter(...)[0] is less efficient than using .find(...). The .find() method stops searching after finding the first match, whereas .filter() iterates through the entire array.

AEconsB0valuAE< =sp ansty="le--0:#D6DEEB;--1:#403F">53< [sp ansty="le--0:#F78C6C;--1:#AA09">8<21sp ansty="le--0:#D6DEEB;--1:#403F">53<, sp ansty="le--0:#F78C6C;--1:#AA09">8<22sp ansty="le--0:#D6DEEB;--1:#403F">53<, sp ansty="le--0:#F78C6C;--1:#AA09">8<23sp ansty="le--0:#D6DEEB;--1:#403F">53<, sp ansty="le--0:#F78C6C;--1:#AA09">8<24sp ansty="le--0:#D6DEEB;--1:#403F">53<, sp ansty="le--0:#F78C6C;--1:#AA09">8<25sp ansty="le--0:#D6DEEB;--1:#403F">53<];d ivclassNa="meec-li">AEconsB0firAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53valuAB0filt5<3(sp ansty="le--0:#D9F5DD;--1:#1111">1<1(sp ansty="le--0:#D7DBE0;--1:#403F">53val1<1)sp ansty="le--0:#C792EA;--1:#8844">AE =<> sp ansty="le--0:#D6DEEB;--1:#403F">53val sp>AE <> sp ansty="le--0:#F78C6C;--1:#AA09">8 sp><23sp ansty="le--0:#D6DEEB;--1:#403F">53<)[sp ansty="le--0:#F78C6C;--1:#AA09">8<20sp ansty="le--0:#D6DEEB;--1:#403F">53<];d ivclassNa="meco"><];d>AEconsB0useAE< =sp ansty="le--0:#D6DEEB;--1:#403F">53 [{ name<: sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4DAli1<1"sp ansty="le--0:#D6DEEB;--1:#403F">53 }, { name<: sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4DB1<1"sp ansty="le--0:#D6DEEB;--1:#403F">53 }<];d ivclassNa="meec-li">AEconsB0usAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53useAB0filt5<3(sp ansty="le--0:#D9F5DD;--1:#1111">1<1(sp ansty="le--0:#D7DBE0;--1:#403F">53us1<1)sp ansty="le--0:#C792EA;--1:#8844">AE =<> sp ansty="le--0:#D6DEEB;--1:#403F">53us sp>A72naAE ==<= sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4DAli1<1"sp ansty="le--0:#D6DEEB;--1:#403F">53<)[sp ansty="le--0:#F78C6C;--1:#AA09">8<20sp ansty="le--0:#D6DEEB;--1:#403F">53<];d ivclassNa="meco"><];d>
44AEco61B0va443F<53 [ spans="tyle--0:#F78C6C;--1:#AA">0<9821 spans="tyle--0:#D6DEEB;--1:#40">3F<53, spans="tyle--0:#F78C6C;--1:#AA">0<9822 spans="tyle--0:#D6DEEB;--1:#40">3F<53, spans="tyle--0:#F78C6C;--1:#AA">0<9823 spans="tyle--0:#D6DEEB;--1:#40">3F<53, spans="tyle--0:#F78C6C;--1:#AA">0<9824 spans="tyle--0:#D6DEEB;--1:#40">3F<53, spans="tyle--0:#F78C6C;--1:#AA">0<9825 spans="tyle--0:#D6DEEB;--1:#40">3F<53] ;divclass="Nameec-">44AEco61B0f44A3F53va4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B031<111( spans="tyle--0:#D7DBE0;--1:#40">3F53v1<111) spans="tyle--0:#C792EA;--1:#88">44AE< => spans="tyle--0:#D6DEEB;--1:#40">3F53v >44A spans="tyle--0:#F78C6C;--1:#AA">0 ><9823 spans="tyle--0:#D6DEEB;--1:#40">3F<53) ;divclass="Name">< 3)><;di vdivclass="Nameexpressive-">44AEco61B0u443F53 [{ na1<111" spans="tyle--0:#ECC48D;--1:#98">4E4DA1<111" spans="tyle--0:#D6DEEB;--1:#40">3F53 }, { na1<111" spans="tyle--0:#ECC48D;--1:#98">4E41<111" spans="tyle--0:#D6DEEB;--1:#40">3F53< }] ;divclass="Nameec-">44AEco61B044A3F53u4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B031<111( spans="tyle--0:#D7DBE0;--1:#40">3F531<111) spans="tyle--0:#C792EA;--1:#88">44AE< => spans="tyle--0:#D6DEEB;--1:#40">3F53 >4<4AE. spans="tyle--0:#7FDBCA;--1:#09">6E7244AE <=== spans="tyle--0:#D9F5DD;--1:#11">1<111" spans="tyle--0:#ECC48D;--1:#98">4E4DA1<111" spans="tyle--0:#D6DEEB;--1:#40">3F<53) ;divclass="Name"><;di vdivclass="Nameexpressive-">44AEco61B0va443F<53 [ spans="tyle--0:#F78C6C;--1:#AA">0<9821 spans="tyle--0:#D6DEEB;--1:#40">3F<53, spans="tyle--0:#F78C6C;--1:#AA">0<9822 spans="tyle--0:#D6DEEB;--1:#40">3F<53, spans="tyle--0:#F78C6C;--1:#AA">0<9823 spans="tyle--0:#D6DEEB;--1:#40">3F<53, spans="tyle--0:#F78C6C;--1:#AA">0<9824 spans="tyle--0:#D6DEEB;--1:#40">3F<53, spans="tyle--0:#F78C6C;--1:#AA">0<9825 spans="tyle--0:#D6DEEB;--1:#40">3F<53] ;divclass="Nameec-">44AEco61B0allPosi44A3F53va4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0fi31<111( spans="tyle--0:#D7DBE0;--1:#40">3F53v1<111) spans="tyle--0:#C792EA;--1:#88">44AE< => spans="tyle--0:#D6DEEB;--1:#40">3F53v >44A spans="tyle--0:#F78C6C;--1:#AA">0 ><9820 spans="tyle--0:#D6DEEB;--1:#40">3F<53) ;divclass="Name">< 0)>

This rule is not configurable.

If you intentionally use .filter(...)[0] to execute side effects in the callback on all array elements before returning the first match, you may want to disable this rule. However, this pattern is unusual and may indicate a need for code refactoring.