2022.2

Table Of Contents
Afunctionusedasatestforeachelementintheset.Filter()passestheiterationindexandthecurrent
elementtothecallbackfunction.Inthescopeofthecallbackfunction,thisreferstothecurrentele-
ment.
Example
Theselectorofthefollowingscriptisli(listitem),sotheresultsobjectcontainsalllistitemsinthe
template.Thescriptsfiltersthethirdandsixthlineitemsfromtheresults,takingadvantageofthe
indexthatispassedtothefilterfunction,andcolorsthemred.Itusesthemodulusoperator(%)to
selecteveryitemwithanindexvaluethat,whendividedby3,hasaremainderof2.(Theindexstarts
countingatzero.)
results.filter(function(index) {
return index % 3 === 2;
}).css( "background-color", "red" );
filter(selector)
Returnsasubsetofaset.Allelementsmatchingtheselectorwillbeincludedintheresult.
Thedifferencebetweenresults.filter(selector)andquery(selector, results)isthat
query()searchesthroughouttheentireresultswhilefilter()onlytakesthetop-levelelementsinto
account.
selector
AStringcontainingaCSSselector.Seehttps://www.w3schools.com/cssref/css_selectors.aspforCSS
selectorsandcombinationsofCSSselectors.
Example
Theselectorofthefollowingscriptistr(tablerow),sotheobjectresultscontainsallrowsinthetem-
plate.Thescriptsfiltersallevenrowsfromtheresultsandcolorsthemred.
results.filter(":nth-child(even)").css("background-color", "red");
find()
find(textToFind)
PerformsadeepsearchfortextToFindinthechildrenofeachelement,andreturnsanewresultset
withelementsthatsurroundtheoccurrences.
textToFind
AStringthatcontainsthesearchtext.
Page 764