Skip to content

arrayFilteredFinds

Reports using .filter() when only the first or last matching element is needed.

✅ This rule is included in the ts logical and logicalStrict presets.

Using .filter() to get only the first or last matching element creates an unnecessary intermediate array. The .find() method is more efficient as it stops iteration once a match is found. Similarly, .findLast() is more efficient for finding the last matching element.

This rule reports:

  • .filter()[0] or .filter().shift() or .filter().at(0) → prefer .find()
  • .filter().pop() or .filter().at(-1) → prefer .findLast()
AEdeclare consB0arr7<2:sp ansty="le--0:#C792EA;--1:#8844">AB0numb53[<];d ivclassNa="meec-li">53arrAB0filt5<3(sp ansty="le--0:#D9F5DD;--1:#1111">1<1(sp ansty="le--0:#D7DBE0;--1:#403F">53val1<1)sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#C792EA;--1:#8844">AE<=>sp ansty="le--0:#D6DEEB;--1:#403F">53 valusp>Asp ansty="le--0:#D6DEEB;--1:#403F">5sp><3 sp ansty="le--0:#F78C6C;--1:#AA09">8<20sp 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>AEdeclare consB0arr7<2:sp ansty="le--0:#C792EA;--1:#8844">AB0numb53[<];d ivclassNa="meec-li">53arrAB0filt5<3(sp ansty="le--0:#D9F5DD;--1:#1111">1<1(sp ansty="le--0:#D7DBE0;--1:#403F">53val1<1)sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#C792EA;--1:#8844">AE<=>sp ansty="le--0:#D6DEEB;--1:#403F">53 valusp>Asp ansty="le--0:#D6DEEB;--1:#403F">5sp><3 sp ansty="le--0:#F78C6C;--1:#AA09">8<20sp ansty="le--0:#D6DEEB;--1:#403F">5<3)sp ansty="le--0:#C792EA;--1:#8844">AB0shi53(<);d ivclassNa="meco"><);d>AEdeclare consB0arr7<2:sp ansty="le--0:#C792EA;--1:#8844">AB0numb53[<];d ivclassNa="meec-li">53arrAB0filt5<3(sp ansty="le--0:#D9F5DD;--1:#1111">1<1(sp ansty="le--0:#D7DBE0;--1:#403F">53val1<1)sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#C792EA;--1:#8844">AE<=>sp ansty="le--0:#D6DEEB;--1:#403F">53 valusp>Asp ansty="le--0:#D6DEEB;--1:#403F">5sp><3 sp ansty="le--0:#F78C6C;--1:#AA09">8<20sp ansty="le--0:#D6DEEB;--1:#403F">5<3)sp ansty="le--0:#C792EA;--1:#8844">AB05<3(sp ansty="le--0:#F78C6C;--1:#AA09">8<20sp ansty="le--0:#D6DEEB;--1:#403F">53<);d ivclassNa="meco"><);d>AEdeclare consB0arr7<2:sp ansty="le--0:#C792EA;--1:#8844">AB0numb53[<];d ivclassNa="meec-li">53arrAB0filt5<3(sp ansty="le--0:#D9F5DD;--1:#1111">1<1(sp ansty="le--0:#D7DBE0;--1:#403F">53val1<1)sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#C792EA;--1:#8844">AE<=>sp ansty="le--0:#D6DEEB;--1:#403F">53 valusp>Asp ansty="le--0:#D6DEEB;--1:#403F">5sp><3 sp ansty="le--0:#F78C6C;--1:#AA09">8<20sp ansty="le--0:#D6DEEB;--1:#403F">5<3)sp ansty="le--0:#C792EA;--1:#8844">AB0p53(<);d ivclassNa="meco"><);d>AEdeclare consB0arr7<2:sp ansty="le--0:#C792EA;--1:#8844">AB0numb53[<];d ivclassNa="meec-li">53arrAB0filt5<3(sp ansty="le--0:#D9F5DD;--1:#1111">1<1(sp ansty="le--0:#D7DBE0;--1:#403F">53val1<1)sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#C792EA;--1:#8844">AE<=>sp ansty="le--0:#D6DEEB;--1:#403F">53 valusp>Asp ansty="le--0:#D6DEEB;--1:#403F">5sp><3 sp ansty="le--0:#F78C6C;--1:#AA09">8<20sp ansty="le--0:#D6DEEB;--1:#403F">5<3)sp ansty="le--0:#C792EA;--1:#8844">AB05<3(sp ansty="le--0:#C792EA;--1:#8844">A8<21sp ansty="le--0:#D6DEEB;--1:#403F">53<);d ivclassNa="meco"><);d>
44AEdeclare co61B0a64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0nu3F5<3[] ;divclass="Nameec-">3F53a4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B031<111( spans="tyle--0:#D7DBE0;--1:#40">3F53v1<111) spans="tyle--0:#D6DEEB;--1:#40">344 spans="tyle--0:#D6DEEB;--1:#40">3F53 va>4<4AE> spans="tyle--0:#D6DEEB;--1:#40">3>0<9820 spans="tyle--0:#D6DEEB;--1:#40">3F<53) ;divclass="Name">< 0)><;di vdivclass="Nameexpressive-">44AEdeclare co61B0a64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0nu3F5<3[] ;divclass="Nameec-">3F53a4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0find31<111( spans="tyle--0:#D7DBE0;--1:#40">3F53v1<111) spans="tyle--0:#D6DEEB;--1:#40">344 spans="tyle--0:#D6DEEB;--1:#40">3F53 va>4<4AE> spans="tyle--0:#D6DEEB;--1:#40">3>0<9820 spans="tyle--0:#D6DEEB;--1:#40">3F<53) ;divclass="Name">< 0)><;di vdivclass="Nameexpressive-">44AEdeclare co61B0a64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0nu3F5<3[] ;divclass="Nameec-">44AEco61B0filt44A3F53a4<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)><;di vdivclass="Nameexpressive-">44AEdeclare co61B0a64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0nu3F5<3[] ;divclass="Nameec-">3F53a4<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0fi31<111( spans="tyle--0:#D7DBE0;--1:#40">3F53v1<111) spans="tyle--0:#D6DEEB;--1:#40">344 spans="tyle--0:#D6DEEB;--1:#40">3F53 va>4<4AE> spans="tyle--0:#D6DEEB;--1:#40">3>0<9820 spans="tyle--0:#D6DEEB;--1:#40">34<4AE. spans="tyle--0:#7FDBCA;--1:#09">6E72le3

This rule is not configurable.

If you need the full filtered array for other purposes beyond accessing the first element, you can disable this rule. However, in most cases where only the first match is needed, .find() is the better choice.