Underscore. js

  • 2022-01-10Abholtermin
  • 2022-02-15Aktualisiert
Underscore. js

Domainnamewww.underscorejs.orgBewertung

etwa 500~20000

Domainnamewww.underscorejs.orgfließen

335

Domainnamewww.underscorejs.orgGut oder schlecht

Großartige Show. Kann erfolgreich sein Ji ein Wohlstand und ein Niedergang. vergebliche Mühe

Webseite:Underscore. jsGewichte

2

Webseite:Underscore. jsIP

104.21.25.112

Webseite:Underscore. jsInhalt

Underscore.jsbody{font-size:14px;line-height:22px;background:#f4f4f4url(docs/imes/background.png);color:#000;font-family:HelveticaNeue,Helvetica,Arial;}.interface{font-family:"LucidaGrande","LucidaSansUnicode",Helvetica,Arial,sans-serif!important;}.container{margin-top:50px;}div#sidebar{background:#fff;position:fixed;top:0;left:0;bottom:0;width:220px;overflow-y:auto;overflow-x:hidden;-webkit-overflow-scrolling:touch;padding:15px030px30px;border-right:1pxsolid#bbb;box-shadow:0020px#ccc;-webkit-box-shadow:0020px#ccc;-moz-box-shadow:0020px#ccc;}a.toc_title,a.toc_title:visited{display:block;color:black;font-weight:bold;margin-top:15px;}a.toc_title:hover{text-decoration:underline;}#sidebar.version{font-size:10px;font-weight:normal;}ul.toc_section{font-size:11px;line-height:14px;margin:5px000;padding-left:0px;list-style-type:none;font-family:LucidaGrande;}.toc_sectionli{cursor:pointer;margin:003px0;}.toc_sectionlia{text-decoration:none;color:black;}.toc_sectionlia:hover{text-decoration:underline;}input#function_filter{width:80%;}div.container{width:550px;margin:40px050px280px;}img#logo{width:396px;height:69px;}div.warning{margin-top:15px;font:bold11pxArial;color:#;}p{margin:20px0;width:550px;}a,a:visited{color:#444;}a:active,a:hover{color:#000;}h1,h2,h3,h4,h5,h6{padding-top:20px;}h2{font-size:20px;}b.header{font-size:16px;line-height:30px;}span.alias{font-size:14px;font-style:italic;margin-left:20px;}a.source{font-variant:small-caps;margin-left:20px;}a.source+a.source{margin-left:unset;}table,tr,td{margin:0;padding:0;}td{padding:2px12px2px0;}table.rule{height:1px;background:#ccc;margin:5px0;}table.dl-link{white-space:nowrap;}ul{list-style-type:circle;padding:00020px;}li{margin-bottom:10px;}code,pre,tt{font-family:Monaco,Consolas,"LucidaConsole",monospace;font-size:12px;line-height:18px;font-style:normal;}tt{padding:0px3px;background:#fff;border:1pxsolid#ddd;zoom:1;}code{margin-left:20px;}pre{font-size:12px;padding:2px02px15px;border-left:5pxsolid#bbb;margin:0px030px;}input[type=checkbox]{position:absolute;top:-9999px;left:-9999px;}label{position:fixed;left:0;display:none;width:1em;height:1.5em;padding:.5em0.5em0;font-size:1.1em;color:#fff;transition:color.3sease-in-out;cursor:pointer;user-select:none;margin:0;background:rgba(0,0,0,1);}.overlay{height:100%;width:0;position:fixed;z-index:1;top:0;left:0;background-color:rgb(0,0,0);background-color:rgba(0,0,0,0.9);overflow-x:hidden;transition:0.5s;}@mediaonlyscreenand(-webkit-min-device-pixel-ratio:1.5)and(max-width:640px),onlyscreenand(-o-min-device-pixel-ratio:3/2)and(max-width:640px),onlyscreenand(min-device-pixel-ratio:1.5)and(max-width:640px){img{max-width:100%;}div#sidebar{-webkit-overflow-scrolling:initial;position:relative;width:100%;height:100%;left:0;padding:20px010px45px;border:0;}img#logo{width:auto;height:auto;}div.container{margin:50px0;width:100%;}p,div.containerul{max-width:98%;overflow-x:scroll;}pre{overflow:scroll;}}@mediascreenand(max-width:640px){body{line-height:22px;}ul.toc_section{font-size:14px;line-height:15px;font-family:HelveticaNeue,Helvetica,Arial;}label{display:block;top:0;z-index:999;}label:after{poUnderscore. jssition:absolute;right:.25em;top:5px;content:"\2261";font-size:1.8em;}label:hover,input:checked~label{color:#fff;}input:checked~#myN{width:0;}#myN{width:100%;}.overlay-content{position:relative;top:25%;width:100%;}}@mediascreenand(prefers-color-scheme:dark){body{background:#111!important;color:#c6c6c6;}div#sidebar{background:#111;border-right:1pxsolid#666;box-shadow:0020px#555;-webkit-box-shadow:0020px#555;-moz-box-shadow:0020px#555;}a.toc_title,a.toc_title:visited{color:#f4f4f4;}a.toc_title:hover{text-decoration:underline;}.toc_sectionlia{text-decoration:none;color:#f4f4f4;}img#logo{filter:invert(1)hue-rotate(180deg)brightness(0.9);}div.warning{color:#CC6060;}a,a:visited{color:#f4f4f4;}a:active,a:hover{color:#ffffff;}table.rule{background:#777;}tt{background:#444;border:1pxsolid#777;}pre{border-left:5pxsolid#aaa;}}Underscore.js(1.13.7)»GitHubRepository»AnnotatedSource(modular)»AnnotatedSource(singleread)»Underscore-contrib»Twitter»Tidelift»PatreonIntroductionCollections-each-map-reduce-reduceRight-find-filter-where-findWhere-reject-every-some-contains-invoke-pluck-max-min-sortBy-groupBy-indexBy-countBy-shuffle-sample-toArray-size-partitionArrays-first-initial-last-rest-compact-flatten-without-union-intersection-difference-uniq-zip-unzip-object-chunk-indexOf-lastIndexOf-sortedIndex-findIndex-findLastIndex-rangeFunctions-bind-bindAll-partial-memoize-delay-defer-throttle-debounce-once-after-before-wrap-negate-compose-restArgumentsObjects-keys-allKeys-values-mapObject-pairs-invert-create-functions-findKey-extend-extendOwn-pick-omit-defaults-clone-tap-toPath-has-get-property-propertyOf-matcher-isEqual-isMatch-isEmpty-isElement-isArray-isObject-isArguments-isFunction-isString-isNumber-isFinite-isBoolean-isDate-isRegExp-isError-isSymbol-isMap-isWeakMap-isSet-isWeakSet-isArrayBuffer-isDatiew-isTypedArray-isNaN-isNull-isUndefinedUtility-noConflict-identity-constant-noop-times-random-mixin-iteratee-uniqueId-escape-unescape-result-now-templateOOPStyleChaining-chain-valueLinksNotesChangeLogUnderscoreisaJaScriptlibrarythatprovidesawholemessofusefulfunctionalprogramminghelperswithoutextendinganybuilt-inobjects.It’stheanswertothequestion:“IfIsitdowninfrontofablankHTMLpe,andwanttostartbeingproductiveimmediately,whatdoIneed?”…andthetietogoalongwithjQuery'stuxandBackbone'ssuspenders.Underscoreprovidesover100functionsthatsupportbothyourforiteworkadayfunctionalhelpers:map,filter,invoke—aswellasmorespecializedgoodies:functionbinding,jascripttemplating,creatingquickindexes,deepequalitytesting,andsoon.AcompleteTestSuiteisincludedforyourperusal.Youmayalsoreadthroughtheannotatedsourcecode.Thereisamodularversionwithclickableimportreferencesaswell.Youmaychoosebetweenmonolithicandmodularimports.Thereisaquicksummaryoftheoptionsbelow,aswellasamorecomprehensivediscussioninthearticle.EnjoyingUnderscore,andwanttoturnitupto11?TryUnderscore-contrib.TheprojectishostedonGitHub.YoucanreportbugsanddiscussfeaturesontheissuespeorchatintheGitterchannel.YoucansupporttheprojectbydonatingonPatreon.EnterprisecovereisailableaspartoftheTideliftSubscription.Underscoreisanopen-sourcecomponentofDocumentCloud.v1.13.7Downloads(Right-click,anduse"SeAs")ESM(Development)65.9KB,UncompressedwithPlentifulComments (SourceMap)ESM(Production)8.59KB,MinifiedandGzipped (SourceMap)UMD(Development)68.4KB,UncompressedwithBountifulComments (SourceMap)UMD(Production)7.48KB,MinifiedandGzipped (SourceMap)EdgeESMUnreleased,currentmaster,usebyyourownjudgementandatyourownriskEdgeUMDUnreleased,currentmaster,useifyou’refeelingluckyv1.13.7CDNURLs(Usewith<scriptsrc="..."></script>)cdn.jsdelivr.net/npm/[email160;protected]/underscore-umd-min.jscdn.jsdelivr.net/npm/[email160;protected]/underscore-esm-min.jsunpkg.com/[email160;protected]/underscore-umd-min.jsunpkg.com/[email160;protected]/underscore-esm-min.jspecdn.io/lib/underscore/1.13.7/underscore-umd-min.jspecdn.io/lib/underscore/1.13.7/underscore-esm-min.jscdnjs.cloudflare.com/ajax/libs/underscore.js/1.13.7/underscore-umd-min.jscdnjs.cloudflare.com/ajax/libs/underscore.js/1.13.7/underscore-esm-min.jsInmostcases,youcanreplacetheversionnumberabovebylatestsothatyourembedwillautomaticallyusethelatestversion,orstableifyouwanttodelayupdatinguntilanupdatehasproventobefreeofaccidentalbreakingchanges.Example:cdn.jsdelivr.net/npm/underscore@latest/underscore-umd-min.jsPackeInstallationNode.jsnpminstallunderscoreMeteor.jsmeteoraddunderscoreBowerbowerinstallunderscoreIfyouarehardcodingthepathtothefilewithinthepackeandyouareunsurewhichbuildtouse,itisverylikelythatyouneedunderscore-umd.jsortheminifiedvariantunderscore-umd-min.js.MonolithicImport(recommended)ESMimport_,{map}from'underscore';AMDrequire(['underscore'],...)CommonJSvar_=require('underscore');ExtendScript#include"underscore-umd.js"ModularImportESMimportmapfrom'underscore/modules/map.js'AMDrequire(['underscore/amd/map.js'],...)CommonJSvarmap=require('underscore/cjs/map.js');Forfunctionswithmultiplealiases,thefilenameofthemoduleisalwaysthefirstnamethatappearsinthedocumentation.Forexample,_.reduce/_.inject/_.foldlisexportedfromunderscore/modules/reduce.js.ModularuseismostlyrecommendedforcreatingacustomizedbuildofUnderscore.EngineCompatibilityUnderscore1.xisbackwardscompatiblewithanyenginethatfullysupportsES3,whilealsoutilizingnewerfeatureswhenailable,suchasObject.keys,typedarraysandESmodules.WeroutinelyrunourunittestsainsttheJaScriptengineslistedbelow:Chrome26–latestEdge13,18andlatestFirefox11–latestInternetExplorer9–11Node.js8–latestLTSSafari8–latestInaddition:WeherecentconfirmationthatthelibraryiscompatiblewithAdobeExtendScript.ThereissupportcodepresentforIE8,whichwewillretaininfutureUnderscore1.xupdates.PatchestoenhancesupportforotherES3-compatibleenvironmentsarealwayswelcome.Underscore2.xwilllikelyremovesupportforsomeoutdatedenvironments.CollectionFunctions(ArraysorObjects)each_.each(list,iteratee,[context])Alias:forEachsourceIteratesoveralistofelements,yieldingeachinturntoaniterateefunction.Theiterateeisboundtothecontextobject,ifoneispassed.Eachinvocationofiterateeiscalledwiththreearguments:(element,index,list).IflistisaJaScriptobject,iteratee'sargumentswillbe(value,key,list).Returnsthelistforchaining._.each([1,2,3],alert);=>alertseachnumberinturn..._.each({one:1,two:2,three:3},alert);=>alertseachnumbervalueinturn...Note:Collectionfunctionsworkonarrays,objects,andarray-likeobjectssuchasarguments,NodeListandsimilar.Butitworksbyduck-typing,sooidpassingobjectswithanumericlengthproperty.It'salsogoodtonotethataneachloopcannotbebrokenoutof—tobreak,use_.findinstead.map_.map(list,iteratee,[context])Alias:collectsourceProducesanewarrayofvaluesbymappingeachvalueinlistthroughatransformationfunction(iteratee).Theiterateeispassedthreearguments:thevalue,thentheindex(orkey)oftheiteration,andfinallyareferencetotheentirelist._.map([1,2,3],function(num){returnnum*3;});=>[3,6,9]_.map({one:1,two:2,three:3},function(num,key){returnnum*3;});=>[3,6,9]_.map([[1,2],[3,4]],_.first);=>[1,3]reduce_.reduce(list,iteratee,[memo],[context])Aliases:inject,foldlsourceAlsoknownasinjectandfoldl,reduceboilsdownalistofvaluesintoasinglevalue.Memoistheinitialstateofthereduction,andeachsuccessivestepofitshouldbereturnedbyiteratee.Theiterateeispassedfourarguments:thememo,thenthevalueandindex(orkey)oftheiteration,andfinallyareferencetotheentirelist.Ifnomemoispassedtotheinitialinvocationofreduce,theiterateeisnotinvokedonthefirstelementofthelist.Thefirstelementisinsteadpassedasthememointheinvocationoftheiterateeonthenextelementinthelist.varsum=_.reduce([1,2,3],function(memo,num){returnmemo+num;},0);=>6reduceRight_.reduceRight(list,iteratee,[memo],[context])Alias:foldrsourceTheright-associativeversionofreduce.FoldrisnotasusefulinJaScriptasitwouldbeinalanguewithlazyevaluation.varlist=[[0,1],[2,3],[4,5]];varflat=_.reduceRight(list,function(a,b){returna.concat(b);},[]);=>[4,5,2,3,0,1]find_.find(list,predicate,[context])Alias:detectsourceLooksthrougheachvalueinthelist,returningthefirstonethatpassesatruthtest(predicate),orundefinedifnovaluepassesthetest.Thefunctionreturnsassoonasitfindsanacceptableelement,anddoesn'ttrersetheentirelist.predicateistransformedthroughiterateetofacilitateshorthandsyntaxes.vareven=_.find([1,2,3,4,5,6],function(num){returnnum%2==0;});=>2filter_.filter(list,predicate,[context])Alias:selectsourceLooksthrougheachvalueinthelist,returninganarrayofallthevaluesthatpassatruthtest(predicate).predicateistransformedthroughiterateetofacilitateshorthandsyntaxes.varevens=_.filter([1,2,3,4,5,6],function(num){returnnum%2==0;});=>[2,4,6]findWhere_.findWhere(list,properties)sourceLooksthroughthelistandreturnsthefirstvaluethatmatchesallofthekey-valuepairslistedinproperties.Ifnomatchisfound,oriflistisempty,undefinedwillbereturned._.findWhere(publicServicePulitzers,{newsroom:"TheNewYorkTimes"});=>{year:1918,newsroom:"TheNewYorkTimes",reason:"Foritspublicserviceinpublishinginfullsomanyofficialreports,documentsandspeechesbyEuropeanstatesmenrelatingtotheprogressandconductofthewar."}where_.where(list,properties)sourceLooksthrougheachvalueinthelist,returninganarrayofallthevaluesthatmatchesthekey-valuepairslistedinproperties._.where(listOfPlays,{author:"Shakespeare",year:1611});=>[{title:"Cymbeline",author:"Shakespeare",year:1611},{title:"TheTempest",author:"Shakespeare",year:1611}]reject_.reject(list,predicate,[context])sourceReturnsthevaluesinlistwithouttheelementsthatthetruthtest(predicate)passes.Theoppositeoffilter.predicateistransformedthroughiterateetofacilitateshorthandsyntaxes.varodds=_.reject([1,2,3,4,5,6],function(num){returnnum%2==0;});=>[1,3,5]every_.every(list,[predicate],[context])Alias:allsourceReturnstrueifallofthevaluesinthelistpassthepredicatetruthtest.Short-circuitsandstopstrersingthelistifafalseelementisfound.predicateistransformedthroughiterateetofacilitateshorthandsyntaxes._.every([2,4,5],function(num){returnnum%2==0;});=>falsesome_.some(list,[predicate],[context])Alias:anysourceReturnstrueifanyofthevaluesinthelistpassthepredicatetruthtest.Short-circuitsandstopstrersingthelistifatrueelementisfound.predicateistransformedthroughiterateetofacilitateshorthandsyntaxes._.some([null,0,'yes',false]);=>truecontains_.contains(list,value,[fromIndex])Aliases:include,includessourceReturnstrueifthevalueispresentinthelist.UsesindexOfinternally,iflistisanArray.UsefromIndextostartyoursearchatagivenindex._.contains([1,2,3],3);=>trueinvoke_.invoke(list,methodName,*arguments)sourceCallsthemethodnamedbymethodNameoneachvalueinthelist.Anyextraargumentspassedtoinvokewillbeforwardedontothemethodinvocation._.invoke([[5,1,7],[3,2,1]],'sort');=>[[1,5,7],[1,2,3]]pluck_.pluck(list,propertyName)sourceAconvenientversionofwhatisperhapsthemostcommonuse-caseformap:extractingalistofpropertyvalues.varstooges=[{name:'moe',e:40},{name:'larry',e:50},{name:'curly',e:60}];_.pluck(stooges,'name');=>["moe","larry","curly"]max_.max(list,[iteratee],[context])sourceReturnsthemaximumvalueinlist.Ifaniterateefunctionisprovided,itwillbeusedoneachvaluetogeneratethecriterionbywhichthevalueisranked.-Infinityisreturnediflistisempty,soanisEmptyguardmayberequired.Thisfunctioncancurrentlyonlycomparenumbersreliably.Thisfunctionusesoperator<(note).varstooges=[{name:'moe',e:40},{name:'larry',e:50},{name:'curly',e:60}];_.max(stooges,function(stooge){returnstooge.e;});=>{name:'curly',e:60};min_.min(list,[iteratee],[context])sourceReturnstheminimumvalueinlist.Ifaniterateefunctionisprovided,itwillbeusedoneachvaluetogeneratethecriterionbywhichthevalueisranked.Infinityisreturnediflistisempty,soanisEmptyguardmayberequired.Thisfunctioncancurrentlyonlycomparenumbersreliably.Thisfunctionusesoperator<(note).varnumbers=[10,5,100,2,1000];_.min(numbers);=>2sortBy_.sortBy(list,iteratee,[context])sourceReturnsa(stably)sortedcopyoflist,rankedinascendingorderbytheresultsofrunningeachvaluethroughiteratee.iterateemayalsobethestringnameofthepropertytosortby(eg.length).Thisfunctionusesoperator<(note)._.sortBy([1,2,3,4,5,6],function(num){returnMath.sin(num);});=>[5,4,6,3,1,2]varstooges=[{name:'moe',e:40},{name:'larry',e:50},{name:'curly',e:60}];_.sortBy(stooges,'name');=>[{name:'curly',e:60},{name:'larry',e:50},{name:'moe',e:40}];groupBy_.groupBy(list,iteratee,[context])sourceSplitsacollectionintosets,groupedbytheresultofrunningeachvaluethroughiteratee.Ifiterateeisastringinsteadofafunction,groupsbythepropertynamedbyiterateeoneachofthevalues._.groupBy([1.3,2.1,2.4],function(num){returnMath.floor(num);});=>{1:[1.3],2:[2.1,2.4]}_.groupBy(['one','two','three'],'length');=>{3:["one","two"],5:["three"]}indexBy_.indexBy(list,iteratee,[context])sourceGivenalist,andaniterateefunctionthatreturnsakeyforeachelementinthelist(orapropertyname),returnsanobjectwithanindexofeachitem.JustlikegroupBy,butforwhenyouknowyourkeysareunique.varstooges=[{name:'moe',e:40},{name:'larry',e:50},{name:'curly',e:60}];_.indexBy(stooges,'e');=>{"40":{name:'moe',e:40},"50":{name:'larry',e:50},"60":{name:'curly',e:60}}countBy_.countBy(list,iteratee,[context])sourceSortsalistintogroupsandreturnsacountforthenumberofobjectsineachgroup.SimilartogroupBy,butinsteadofreturningalistofvalues,returnsacountforthenumberofvaluesinthatgroup._.countBy([1,2,3,4,5],function(num){returnnum%2==0?'even':'odd';});=>{odd:3,even:2}shuffle_.shuffle(list)sourceReturnsashuffledcopyofthelist,usingaversionoftheFisher-Yatesshuffle._.shuffle([1,2,3,4,5,6]);=>[4,1,6,3,5,2]sample_.sample(list,[n])sourceProducearandomsamplefromthelist.Passanumbertoreturnnrandomelementsfromthelist.Otherwiseasinglerandomitemwillbereturned._.sample([1,2,3,4,5,6]);=>4_.sample([1,2,3,4,5,6],3);=>[1,6,2]toArray_.toArray(list)sourceCreatesarealArrayfromthelist(anythingthatcanbeiteratedover).Usefulfortransmutingtheargumentsobject.(function(){return_.toArray(arguments).slice(1);})(1,2,3,4);=>[2,3,4]size_.size(list)sourceReturnthenumberofvaluesinthelist._.size([1,2,3,4,5]);=>5_.size({one:1,two:2,three:3});=>3partition_.partition(list,predicate)sourceSplitlistintotwoarrays:onewhoseelementsallsatisfypredicateandonewhoseelementsalldonotsatisfypredicate.predicateistransformedthroughiterateetofacilitateshorthandsyntaxes._.partition([0,1,2,3,4,5],isOdd);=>[[1,3,5],[0,2,4]]compact_.compact(list)sourceReturnsacopyofthelistwithallfalsyvaluesremoved.InJaScript,false,null,0,"",undefinedandNaNareallfalsy._.compact([0,1,false,2,'',3]);=>[1,2,3]ArrayFunctionsNote:Allarrayfunctionswillalsoworkontheargumentsobject.However,Underscorefunctionsarenotdesignedtoworkon"sparse"arrays.first_.first(array,[n])Aliases:head,takesourceReturnsthefirstelementofanarray.Passingnwillreturnthefirstnelementsofthearray._.first([5,4,3,2,1]);=>5initial_.initial(array,[n])sourceReturnseverythingbutthelastentryofthearray.Especiallyusefulontheargumentsobject.Passntoexcludethelastnelementsfromtheresult._.initial([5,4,3,2,1]);=>[5,4,3,2]last_.last(array,[n])sourceReturnsthelastelementofanarray.Passingnwillreturnthelastnelementsofthearray._.last([5,4,3,2,1]);=>1rest_.rest(array,[index])Aliases:tail,dropsourceReturnstherestoftheelementsinanarray.Passanindextoreturnthevaluesofthearrayfromthatindexonward._.rest([5,4,3,2,1]);=>[4,3,2,1]flatten_.flatten(array,[depth])sourceFlattensanestedarray.Ifyoupasstrueor1asthedepth,thearraywillonlybeflattenedasinglelevel.Passingagreaternumberwillcausetheflatteningtodescenddeeperintothenestinghierarchy.Omittingthedepthargument,orpassingfalseorInfinity,flattensthearrayallthewaytothedeepestnestinglevel._.flatten([1,[2],[3,[[4]]]]);=>[1,2,3,4];_.flatten([1,[2],[3,[[4]]]],true);=>[1,2,3,[[4]]];_.flatten([1,[2],[3,[[4]]]],2);=>[1,2,3,[4]];without_.without(array,*values)sourceReturnsacopyofthearraywithallinstancesofthevaluesremoved._.without([1,2,1,0,3,1,4],0,1);=>[2,3,4]union_.union(*arrays)sourceComputestheunionofthepassed-inarrays:thelistofuniqueitems,inorder,thatarepresentinoneormoreofthearrays._.union([1,2,3],[101,2,1,10],[2,1]);=>[1,2,3,101,10]intersection_.intersection(*arrays)sourceComputesthelistofvaluesthataretheintersectionofallthearrays.Eachvalueintheresultispresentineachofthearrays._.intersection([1,2,3],[101,2,1,10],[2,1]);=>[1,2]difference_.difference(array,*others)sourceSimilartowithout,butreturnsthevaluesfromarraythatarenotpresentintheotherarrays._.difference([1,2,3,4,5],[5,2,10]);=>[1,3,4]uniq_.uniq(array,[isSorted],[iteratee])Alias:uniquesourceProducesaduplicate-freeversionofthearray,using===totestobjectequality.Inparticularonlythefirstoccurrenceofeachvalueiskept.Ifyouknowinadvancethatthearrayissorted,passingtrueforisSortedwillrunamuchfasteralgorithm.Ifyouwanttocomputeuniqueitemsbasedonatransformation,passaniterateefunction._.uniq([1,2,1,4,1,3]);=>[1,2,4,3]zip_.zip(*arrays)sourceMergestogetherthevaluesofeachofthearrayswiththevaluesatthecorrespondingposition.Usefulwhenyouheseparatedatasourcesthatarecoordinatedthroughmatchingarrayindexes._.zip(['moe','larry','curly'],[30,40,50],[true,false,false]);=>[["moe",30,true],["larry",40,false],["curly",50,false]]unzip_.unzip(array)Alias:transposesourceTheoppositeofzip.Givenanarrayofarrays,returnsaseriesofnewarrays,thefirstofwhichcontainsallofthefirstelementsintheinputarrays,thesecondofwhichcontainsallofthesecondelements,andsoon.Ifyou'reworkingwithamatrixofnestedarrays,thiscanbeusedtotransposethematrix._.unzip([["moe",30,true],["larry",40,false],["curly",50,false]]);=>[['moe','larry','curly'],[30,40,50],[true,false,false]]object_.object(list,[values])sourceConvertsarraysintoobjects.Passeitherasinglelistof[key,value]pairs,oralistofkeys,andalistofvalues.Passingbypairsisthereverseofpairs.Ifduplicatekeysexist,thelastvaluewins._.object(['moe','larry','curly'],[30,40,50]);=>{moe:30,larry:40,curly:50}_.object([['moe',30],['larry',40],['curly',50]]);=>{moe:30,larry:40,curly:50}chunk_.chunk(array,length)sourceChunksanarrayintomultiplearrays,eachcontaininglengthorfeweritems.varpartners=_.chunk(_.shuffle(kindergarten),2);=>[["Tyrone","Elie"],["Aidan","Sam"],["Katrina","Billie"],["LittleTimmy"]]indexOf_.indexOf(array,value,[isSorted])sourceReturnstheindexatwhichvaluecanbefoundinthearray,or-1ifvalueisnotpresentinthearray.Ifyou'reworkingwithalargearray,andyouknowthatthearrayisalreadysorted,passtrueforisSortedtouseafasterbinarysearch...or,passanumberasthethirdargumentinordertolookforthefirstmatchingvalueinthearrayafterthegivenindex.IfisSortedistrue,thisfunctionusesoperator<(note)._.indexOf([1,2,3],2);=>1lastIndexOf_.lastIndexOf(array,value,[fromIndex])sourceReturnstheindexofthelastoccurrenceofvalueinthearray,or-1ifvalueisnotpresent.PassfromIndextostartyoursearchatagivenindex._.lastIndexOf([1,2,3,1,2,3],2);=>4sortedIndex_.sortedIndex(array,value,[iteratee],[context])sourceUsesabinarysearchtodeterminethesmallestindexatwhichthevalueshouldbeinsertedintothearrayinordertomaintainthearray'ssortedorder.Ifaniterateefunctionisprovided,itwillbeusedtocomputethesortrankingofeachvalue,includingthevalueyoupass.Theiterateemayalsobethestringnameofthepropertytosortby(eg.length).Thisfunctionusesoperator<(note)._.sortedIndex([10,20,30,40,50],35);=>3varstooges=[{name:'moe',e:40},{name:'curly',e:60}];_.sortedIndex(stooges,{name:'larry',e:50},'e');=>1findIndex_.findIndex(array,predicate,[context])sourceSimilarto_.indexOf,returnsthefirstindexwherethepredicatetruthtestpasses;otherwisereturns-1._.findIndex([4,6,8,12],isPrime);=>-1//notfound_.findIndex([4,6,7,12],isPrime);=>2findLastIndex_.findLastIndex(array,predicate,[context])sourceLike_.findIndexbutiteratesthearrayinreverse,returningtheindexclosesttotheendwherethepredicatetruthtestpasses.varusers=[{'id':1,'name':'Bob','last':'Brown'},{'id':2,'name':'Ted','last':'White'},{'id':3,'name':'Frank','last':'James'},{'id':4,'name':'Ted','last':'Jones'}];_.findLastIndex(users,{name:'Ted'});=>3range_.range([start],stop,[step])sourceAfunctiontocreateflexibly-numberedlistsofintegers,handyforeachandmaploops.start,ifomitted,defaultsto0;stepdefaultsto1ifstartisbeforestop,otherwise-1.Returnsalistofintegersfromstart(inclusive)tostop(exclusive),incremented(ordecremented)bystep._.range(10);=>[0,1,2,3,4,5,6,7,8,9]_.range(1,11);=>[1,2,3,4,5,6,7,8,9,10]_.range(0,30,5);=>[0,5,10,15,20,25]_.range(0,-10,-1);=>[0,-1,-2,-3,-4,-5,-6,-7,-8,-9]_.range(0);=>[]Function(uh,ahem)Functionsbind_.bind(function,object,*arguments)sourceBindafunctiontoanobject,meaningthatwheneverthefunctioniscalled,thevalueofthiswillbetheobject.Optionally,passargumentstothefunctiontopre-fillthem,alsoknownaspartialapplication.Forpartialapplicationwithoutcontextbinding,usepartial.varfunc=function(greeting){returngreeting+':'+this.name};func=_.bind(func,{name:'moe'},'hi');func();=>'hi:moe'bindAll_.bindAll(object,*methodNames)sourceBindsanumberofmethodsontheobject,specifiedbymethodNames,toberuninthecontextofthatobjectwhenevertheyareinvoked.Veryhandyforbindingfunctionsthataregoingtobeusedaseventhandlers,whichwouldotherwisebeinvokedwithafairlyuselessthis.methodNamesarerequired.varbuttonView={label:'underscore',onClick:function(){alert('clicked:'+this.label);},onHover:function(){console.log('hovering:'+this.label);}};_.bindAll(buttonView,'onClick','onHover');//Whenthebuttonisclicked,this.labelwillhethecorrectvalue.jQuery('#underscore_button').on('click',buttonView.onClick);partial_.partial(function,*arguments)sourcePartiallyapplyafunctionbyfillinginanynumberofitsarguments,withoutchangingitsdynamicthisvalue.Aclosecousinofbind.Youmaypass_inyourlistofargumentstospecifyanargumentthatshouldnotbepre-filled,butleftopentosupplyatcall-time.Note:ifyouneed_placeholdersandathisbindingatthesametime,useboth_.partialand_.bind.varsubtract=function(a,b){returnb-a;};sub5=_.partial(subtract,5);sub5(20);=>15//UsingaplaceholdersubFrom20=_.partial(subtract,_,20);subFrom20(5);=>15memoize_.memoize(function,[hashFunction])sourceMemoizesagivenfunctionbycachingthecomputedresult.Usefulforspeedingupslow-runningcomputations.IfpassedanoptionalhashFunction,itwillbeusedtocomputethehashkeyforstoringtheresult,basedontheargumentstotheoriginalfunction.ThedefaulthashFunctionjustusesthefirstargumenttothememoizedfunctionasthekey.Thecacheofmemoizedvaluesisailableasthecachepropertyonthereturnedfunction.varfibonacci=_.memoize(function(n){returnn<2?n:fibonacci(n-1)+fibonacci(n-2);});delay_.delay(function,wait,*arguments)sourceMuchlikesetTimeout,invokesfunctionafterwaitmilliseconds.Ifyoupasstheoptionalarguments,theywillbeforwardedontothefunctionwhenitisinvoked.varlog=_.bind(console.log,console);_.delay(log,1000,'loggedlater');=>'loggedlater'//Appearsafteronesecond.defer_.defer(function,*arguments)sourceDefersinvokingthefunctionuntilthecurrentcallstackhascleared,similartousingsetTimeoutwithadelayof0.UsefulforperformingexpensivecomputationsorHTMLrenderinginchunkswithoutblockingtheUIthreadfromupdating.Ifyoupasstheoptionalarguments,theywillbeforwardedontothefunctionwhenitisinvoked._.defer(function(){alert('deferred');});//Returnsfromthefunctionbeforethealertruns.throttle_.throttle(function,wait,[options])sourceCreatesandreturnsanew,throttledversionofthepassedfunction,that,wheninvokedrepeatedly,willonlyactuallycalltheoriginalfunctionatmostoncepereverywaitmilliseconds.Usefulforrate-limitingeventsthatoccurfasterthanyoucankeepupwith.Bydefault,throttlewillexecutethefunctionassoonasyoucallitforthefirsttime,and,ifyoucallitainanynumberoftimesduringthewaitperiod,assoonasthatperiodisover.Ifyou'dliketodisabletheleading-edgecall,pass{leading:false},andifyou'dliketodisabletheexecutiononthetrailing-edge,pass{trailing:false}.varthrottled=_.throttle(updatePosition,100);$(window).scroll(throttled);Ifyouneedtocancelascheduledthrottle,youcancall.cancel()onthethrottledfunction.debounce_.debounce(function,wait,[immediate])sourceCreatesandreturnsanewdebouncedversionofthepassedfunctionwhichwillpostponeitsexecutionuntilafterwaitmillisecondsheelapsedsincethelasttimeitwasinvoked.Usefulforimplementingbehiorthatshouldonlyhappenaftertheinputhasstoppedarriving.Forexample:renderingapreviewofaMarkdowncomment,recalculatingalayoutafterthewindowhasstoppedbeingresized,andsoon.Attheendofthewaitinterval,thefunctionwillbecalledwiththeargumentsthatwerepassedmostrecentlytothedebouncedfunction.Passtruefortheimmediateargumenttocausedebouncetotriggerthefunctionontheleadinginsteadofthetrailingedgeofthewaitinterval.Usefulincircumstanceslikepreventingaccidentaldouble-clicksona"submit"buttonfromfiringasecondtime.varlazyLayout=_.debounce(calculateLayout,300);$(window).resize(lazyLayout);Ifyouneedtocancelascheduleddebounce,youcancall.cancel()onthedebouncedfunction.once_.once(function)sourceCreatesaversionofthefunctionthatcanonlybecalledonetime.Repeatedcallstothemodifiedfunctionwillhenoeffect,returningthevaluefromtheoriginalcall.Usefulforinitializationfunctions,insteadofhingtosetabooleanflandthencheckitlater.varinitialize=_.once(createApplication);initialize();initialize();//Applicationisonlycreatedonce.after_.after(count,function)sourceCreatesawrapperoffunctionthatdoesnothingatfirst.Fromthecount-thcallonwards,itstartsactuallycallingfunction.Usefulforgroupingasynchronousresponses,whereyouwanttobesurethatalltheasynccallshefinished,beforeproceeding.varrenderNotes=_.after(notes.length,render);_.each(notes,function(note){note.asyncSe({success:renderNotes});});//renderNotesisrunonce,afterallnoteshesed.before_.before(count,function)sourceCreatesawrapperoffunctionthatmemoizesitsreturnvalue.Fromthecount-thcallonwards,thememoizedresultofthelastinvocationisreturnedimmediatelyinsteadofinvokingfunctionain.Sothewrapperwillinvokefunctionatmostcount-1times.varmonthlyMeeting=_.before(3,aorRaise);monthlyMeeting();monthlyMeeting();monthlyMeeting();//theresultofanysubsequentcallsisthesameasthesecondcallwrap_.wrap(function,wrapper)sourceWrapsthefirstfunctioninsideofthewrapperfunction,passingitasthefirstargument.Thisallowsthewrappertoexecutecodebeforeandafterthefunctionruns,adjustthearguments,andexecuteitconditionally.varhello=function(name){return"hello:"+name;};hello=_.wrap(hello,function(func){return"before,"+func("moe")+",after";});hello();=>'before,hello:moe,after'negate_.negate(predicate)sourceReturnsanewnegatedversionofthepredicatefunction.varisFalsy=_.negate(Boolean);_.find([-2,-1,0,1,2],isFalsy);=>0compose_.compose(*functions)sourceReturnsthecompositionofalistoffunctions,whereeachfunctionconsumesthereturnvalueofthefunctionthatfollows.Inmathterms,composingthefunctionsf(),g(),andh()producesf(g(h())).vargreet=function(name){return"hi:"+name;};varexclaim=function(statement){returnstatement.toUpperCase()+"!";};varwelcome=_.compose(greet,exclaim);welcome('moe');=>'hi:MOE!'restArguments_.restArguments(function,[startIndex])sourceReturnsaversionofthefunctionthat,whencalled,receivesallargumentsfromandbeyondstartIndexcollectedintoasinglearray.Ifyoudon’tpassanexplicitstartIndex,itwillbedeterminedbylookingatthenumberofargumentstothefunctionitself.SimilartoES6’srestparameterssyntax.varraceResults=_.restArguments(function(gold,silver,bronze,everyoneElse){_.each(everyoneElse,sendConsolations);});raceResults("Dopey","Grumpy","Happy","Sneezy","Bashful","Sleepy","Doc");ObjectFunctionskeys_.keys(object)sourceRetrieveallthenamesoftheobject'sownenumerableproperties._.keys({one:1,two:2,three:3});=>["one","two","three"]allKeys_.allKeys(object)sourceRetrieveallthenamesofobject'sownandinheritedproperties.functionStooge(name){this.name=name;}Stooge.prototype.silly=true;_.allKeys(newStooge("Moe"));=>["name","silly"]values_.values(object)sourceReturnallofthevaluesoftheobject'sownproperties._.values({one:1,two:2,three:3});=>[1,2,3]mapObject_.mapObject(object,iteratee,[context])sourceLikemap,butforobjects.Transformthevalueofeachpropertyinturn._.mapObject({start:5,end:12},function(val,key){returnval+5;});=>{start:10,end:17}pairs_.pairs(object)sourceConvertanobjectintoalistof[key,value]pairs.Theoppositeofobject._.pairs({one:1,two:2,three:3});=>[["one",1],["two",2],["three",3]]invert_.invert(object)sourceReturnsacopyoftheobjectwherethekeyshebecomethevaluesandthevaluesthekeys.Forthistowork,allofyourobject'svaluesshouldbeuniqueandstringserializable._.invert({Moe:"Moses",Larry:"Louis",Curly:"Jerome"});=>{Moses:"Moe",Louis:"Larry",Jerome:"Curly"};create_.create(prototype,props)sourceCreatesanewobjectwiththegivenprototype,optionallyattachingpropsasownproperties.Basically,Object.create,butwithoutallofthepropertydescriptorjazz.varmoe=_.create(Stooge.prototype,{name:"Moe"});functions_.functions(object)Alias:methodssourceReturnsasortedlistofthenamesofeverymethodinanobject—thatistosay,thenameofeveryfunctionpropertyoftheobject._.functions(_);=>["all","any","bind","bindAll","clone","compact","compose"...findKey_.findKey(object,predicate,[context])sourceSimilarto_.findIndexbutforkeysinobjects.Returnsthekeywherethepredicatetruthtestpassesorundefined.predicateistransformedthroughiterateetofacilitateshorthandsyntaxes.extend_.extend(destination,*sources)sourceShallowlycopyallofthepropertiesinthesourceobjectsovertothedestinationobject,andreturnthedestinationobject.Anynestedobjectsorarrayswillbecopiedbyreference,notduplicated.It'sin-order,sothelastsourcewilloverridepropertiesofthesamenameinpreviousarguments._.extend({name:'moe'},{e:50});=>{name:'moe',e:50}extendOwn_.extendOwn(destination,*sources)Alias:assignsourceLikeextend,butonlycopiesownpropertiesovertothedestinationobject.pick_.pick(object,*keys)sourceReturnacopyoftheobject,filteredtoonlyhevaluesfortheallowedkeys(orarrayofvalidkeys).Alternativelyacceptsapredicateindicatingwhichkeystopick._.pick({name:'moe',e:50,userid:'moe1'},'name','e');=>{name:'moe',e:50}_.pick({name:'moe',e:50,userid:'moe1'},function(value,key,object){return_.isNumber(value);});=>{e:50}omit_.omit(object,*keys)sourceReturnacopyoftheobject,filteredtoomitthedisallowedkeys(orarrayofkeys).Alternativelyacceptsapredicateindicatingwhichkeystoomit._.omit({name:'moe',e:50,userid:'moe1'},'userid');=>{name:'moe',e:50}_.omit({name:'moe',e:50,userid:'moe1'},function(value,key,object){return_.isNumber(value);});=>{name:'moe',userid:'moe1'}defaults_.defaults(object,*defaults)sourceReturnsobjectafterfillinginitsundefinedpropertieswiththefirstvaluepresentinthefollowinglistofdefaultsobjects.variceCream={flor:"chocolate"};_.defaults(iceCream,{flor:"vanilla",sprinkles:"lots"});=>{flor:"chocolate",sprinkles:"lots"}clone_.clone(object)sourceCreateashallow-copiedcloneoftheprovidedplainobject.Anynestedobjectsorarrayswillbecopiedbyreference,notduplicated._.clone({name:'moe'});=>{name:'moe'};tap_.tap(object,interceptor)sourceInvokesinterceptorwiththeobject,andthenreturnsobject.Theprimarypurposeofthismethodisto"tapinto"amethodchain,inordertoperformoperationsonintermediateresultswithinthechain._.chain([1,2,3,200]).filter(function(num){returnnum%2==0;}).tap(alert).map(function(num){returnnum*num}).value();=>//[2,200](alerted)=>[4,]toPath_.toPath(path)sourceEnsuresthatpathisanarray.Ifpathisastring,itiswrappedinasingle-elementarray;ifitisanarrayalready,itisreturnedunmodified._.toPath('key');=>['key']_.toPath(['a',0,'b']);=>['a',0,'b']//(samearray)_.toPathisusedinternallyinhas,get,invoke,property,propertyOfandresult,aswellasiniterateeandallfunctionsthatdependonit,inordertonormalizedeeppropertypaths.Youcanoverride_.toPathifyouwanttocustomizethisbehior,forexampletoenableLodash-likestringpathshorthands.Beadvisedthataltering_.toPathwillunoidablycausesomekeystobecomeunreachable;overrideatyourownrisk.//Supportdottedpathshorthands.varoriginalToPath=_.toPath;_.mixin({toPath:function(path){return_.isString(path)?path.split('.'):originalToPath(path);}});_.get({a:[{b:5}]},'a.0.b');=>5get_.get(object,path,[default])sourceReturnsthespecifiedpropertyofobject.pathmaybespecifiedasasimplekey,orasanarrayofobjectkeysorarrayindexes,fordeeppropertyfetching.Ifthepropertydoesnotexistorisundefined,theoptionaldefaultisreturned._.get({a:10},'a');=>10_.get({a:[{b:2}]},['a',0,'b']);=>2_.get({a:10},'b',100);=>100has_.has(object,key)sourceDoestheobjectcontainthegivenkey?Identicaltoobject.hasOwnProperty(key),butusesasafereferencetothehasOwnPropertyfunction,incaseit'sbeenoverriddenaccidentally._.has({a:1,b:2,c:3},"b");=>trueproperty_.property(path)sourceReturnsafunctionthatwillreturnthespecifiedpropertyofanypassed-inobject.pathmaybespecifiedasasimplekey,orasanarrayofobjectkeysorarrayindexes,fordeeppropertyfetching.varstooge={name:'moe'};'moe'===_.property('name')(stooge);=>truevarstooges={moe:{fears:{worst:'Spiders'}},curly:{fears:{worst:'Moe'}}};varcurlysWorstFear=_.property(['curly','fears','worst']);curlysWorstFear(stooges);=>'Moe'propertyOf_.propertyOf(object)sourceInverseof_.property.Takesanobjectandreturnsafunctionwhichwillreturnthevalueofaprovidedproperty.varstooge={name:'moe'};_.propertyOf(stooge)('name');=>'moe'matcher_.matcher(attrs)Alias:matchessourceReturnsapredicatefunctionthatwilltellyouifapassedinobjectcontainsallofthekey/valuepropertiespresentinattrs.varready=_.matcher({selected:true,visible:true});varreadyToGoList=_.filter(list,ready);isEqual_.isEqual(object,other)sourcePerformsanoptimizeddeepcomparisonbetweenthetwoobjects,todetermineiftheyshouldbeconsideredequal.varstooge={name:'moe',luckyNumbers:[13,27,34]};varclone={name:'moe',luckyNumbers:[13,27,34]};stooge==clone;=>false_.isEqual(stooge,clone);=>trueisMatch_.isMatch(object,properties)sourceTellsyouifthekeysandvaluesinpropertiesarecontainedinobject.varstooge={name:'moe',e:32};_.isMatch(stooge,{e:32});=>trueisEmpty_.isEmpty(collection)sourceReturnstrueifcollectionhasnoelements.Forstringsandarray-likeobjects_.isEmptychecksifthelengthpropertyis0.Forotherobjects,itreturnstrueiftheobjecthasnoenumerableown-properties.Notethatprimitivenumbers,booleansandsymbolsarealwaysemptybythisdefinition._.isEmpty([1,2,3]);=>false_.isEmpty({});=>trueisElement_.isElement(object)sourceReturnstrueifobjectisaDOMelement._.isElement(jQuery('body')[0]);=>trueisArray_.isArray(object)sourceReturnstrueifobjectisanArray.(function(){return_.isArray(arguments);})();=>false_.isArray([1,2,3]);=>trueisObject_.isObject(value)sourceReturnstrueifvalueisanObject.NotethatJaScriptarraysandfunctionsareobjects,while(normal)stringsandnumbersarenot._.isObject({});=>true_.isObject(1);=>falseisArguments_.isArguments(object)sourceReturnstrueifobjectisanArgumentsobject.(function(){return_.isArguments(arguments);})(1,2,3);=>true_.isArguments([1,2,3]);=>falseisFunction_.isFunction(object)sourceReturnstrueifobjectisaFunction._.isFunction(alert);=>trueisString_.isString(object)sourceReturnstrueifobjectisaString._.isString("moe");=>trueisNumber_.isNumber(object)sourceReturnstrueifobjectisaNumber(includingNaN)._.isNumber(8.4*5);=>trueisFinite_.isFinite(object)sourceReturnstrueifobjectisafiniteNumber._.isFinite(-101);=>true_.isFinite(-Infinity);=>falseisBoolean_.isBoolean(object)sourceReturnstrueifobjectiseithertrueorfalse._.isBoolean(null);=>falseisDate_.isDate(object)sourceReturnstrueifobjectisaDate._.isDate(newDate());=>trueisRegExp_.isRegExp(object)sourceReturnstrueifobjectisaRegExp._.isRegExp(/moe/);=>trueisError_.isError(object)sourceReturnstrueifobjectinheritsfromanError.try{thrownewTypeError("Example");}catch(o_O){_.isError(o_O);}=>trueisSymbol_.isSymbol(object)sourceReturnstrueifobjectisaSymbol._.isSymbol(Symbol());=>trueisMap_.isMap(object)sourceReturnstrueifobjectisaMap._.isMap(newMap());=>trueisWeakMap_.isWeakMap(object)sourceReturnstrueifobjectisaWeakMap._.isWeakMap(newWeakMap());=>trueisSet_.isSet(object)sourceReturnstrueifobjectisaSet._.isSet(newSet());=>trueisWeakSet_.isWeakSet(object)sourceReturnstrueifobjectisaWeakSet._.isWeakSet(WeakSet());=>trueisArrayBuffer_.isArrayBuffer(object)sourceReturnstrueifobjectisanArrayBuffer._.isArrayBuffer(newArrayBuffer(8));=>trueisDatiew_.isDatiew(object)sourceReturnstrueifobjectisaDatiew._.isDatiew(newDatiew(newArrayBuffer(8)));=>trueisTypedArray_.isTypedArray(object)sourceReturnstrueifobjectisaTypedArray._.isTypedArray(newInt8Array(8));=>trueisNaN_.isNaN(object)sourceReturnstrueifobjectisNaN.Note:thisisnotthesameasthenativeisNaNfunction,whichwillalsoreturntrueformanyothernot-numbervalues,suchasundefined._.isNaN(NaN);=>trueisNaN(undefined);=>true_.isNaN(undefined);=>falseisNull_.isNull(object)sourceReturnstrueifthevalueofobjectisnull._.isNull(null);=>true_.isNull(undefined);=>falseisUndefined_.isUndefined(value)sourceReturnstrueifvalueisundefined._.isUndefined(window.missingVariable);=>trueUtilityFunctionsnoConflict_.noConflict()sourceGivecontroloftheglobal_variablebacktoitspreviousowner.ReturnsareferencetotheUnderscoreobject.varunderscore=_.noConflict();The_.noConflictfunctionisnotpresentifyouusetheEcmaScript6,AMDorCommonJSmodulesystemtoimportUnderscore.identity_.identity(value)sourceReturnsthesamevaluethatisusedastheargument.Inmath:f(x)=xThisfunctionlooksuseless,butisusedthroughoutUnderscoreasadefaultiteratee.varstooge={name:'moe'};stooge===_.identity(stooge);=>trueconstant_.constant(value)sourceCreatesafunctionthatreturnsthesamevaluethatisusedastheargumentof_.constant.varstooge={name:'moe'};stooge===_.constant(stooge)();=>truenoop_.noop()sourceReturnsundefinedirrespectiveoftheargumentspassedtoit.Usefulasthedefaultforoptionalcallbackarguments.obj.initialize=_.noop;times_.times(n,iteratee,[context])sourceInvokesthegiveniterateefunctionntimes.Eachinvocationofiterateeiscalledwithanindexargument.Producesanarrayofthereturnedvalues._.times(3,function(n){genie.grantWishNumber(n);});random_.random(min,max)sourceReturnsarandomintegerbetweenminandmax,inclusive.Ifyouonlypassoneargument,itwillreturnanumberbetween0andthatnumber._.random(0,100);=>42mixin_.mixin(object)sourceAllowsyoutoextendUnderscorewithyourownutilityfunctions.Passahashof{name:function}definitionstoheyourfunctionsaddedtotheUnderscoreobject,aswellastheOOPwrapper.ReturnstheUnderscoreobjecttofacilitatechaining._.mixin({capitalize:function(string){returnstring.charAt(0).toUpperCase()+string.substring(1).toLowerCase();}});_("fabio").capitalize();=>"Fabio"iteratee_.iteratee(value,[context])sourceGeneratesacallbackthatcanbeappliedtoeachelementinacollection._.iterateesupportsanumberofshorthandsyntaxesforcommoncallbackusecases.Dependinguponvalue'stype,_.iterateewillreturn://Novalue_.iteratee();=>_.identity()//Function_.iteratee(function(n){returnn*2;});=>function(n){returnn*2;}//Object_.iteratee({firstName:'Chelsea'});=>_.matcher({firstName:'Chelsea'});//Anythingelse_.iteratee('firstName');=>_.property('firstName');ThefollowingUnderscoremethodstransformtheirpredicatesthrough_.iteratee:countBy,every,filter,find,findIndex,findKey,findLastIndex,groupBy,indexBy,map,mapObject,max,min,partition,reject,some,sortBy,sortedIndex,anduniqYoumayoverwrite_.iterateewithyourowncustomfunction,ifyouwantadditionalordifferentshorthandsyntaxes://Support`RegExp`predicateshorthand.varbuiltinIteratee=_.iteratee;_.iteratee=function(value,context){if(_.isRegExp(value))returnfunction(obj){returnvalue.test(obj)};returnbuiltinIteratee(value,context);};uniqueId_.uniqueId([prefix])sourceGenerateaglobally-uniqueidforclient-sidemodelsorDOMelementsthatneedone.Ifprefixispassed,theidwillbeappendedtoit._.uniqueId('contact_');=>'contact_104'escape_.escape(string)sourceEscapesastringforinsertionintoHTML,replacing&,<,>,",x60;,andx27;characters._.escape('Curly,Larry&Moe');=>"Curly,Larry&amp;Moe"unescape_.unescape(string)sourceTheoppositeofescape,replaces&amp;,&lt;,&gt;,&quot;,&#x60;and&#x27;withtheirunescapedcounterparts._.unescape('Curly,Larry&amp;Moe');=>"Curly,Larry&Moe"result_.result(object,property,[defaultValue])sourceIfthevalueofthenamedpropertyisafunctiontheninvokeitwiththeobjectascontext;otherwise,returnit.Ifadefaultvalueisprovidedandthepropertydoesn'texistorisundefinedthenthedefaultwillbereturned.IfdefaultValueisafunctionitsresultwillbereturned.varobject={cheese:'crumpets',stuff:function(){return'nonsense';}};_.result(object,'cheese');=>"crumpets"_.result(object,'stuff');=>"nonsense"_.result(object,'meat','ham');=>"ham"now_.now()sourceReturnsanintegertimestampforthecurrenttime,usingthefastestmethodailableintheruntime.Usefulforimplementingtiming/animationfunctions._.now();=>51template_.template(templateString,[settings])sourceCompilesJaScripttemplatesintofunctionsthatcanbeevaluatedforrendering.UsefulforrenderingcomplicatedbitsofHTMLfromJSONdatasources.Templatefunctionscanbothinterpolatevalues,using<%=…%>,aswellasexecutearbitraryJaScriptcode,with<%…%>.Ifyouwishtointerpolateavalue,andheitbeHTML-escaped,use<%-…%>.Whenyouevaluateatemplatefunction,passinadataobjectthathaspropertiescorrespondingtothetemplate'sfreevariables.Thesettingsargumentshouldbeahashcontainingany_.templateSettingsthatshouldbeoverridden.varcompiled=_.template("hello:<%=name%>");compiled({name:'moe'});=>"hello:moe"vartemplate=_.template("<b><%-value%></b>");template({value:'<script>'});=>"<b>&lt;script&gt;</b>"YoucanalsouseprintfromwithinJaScriptcode.Thisissometimesmoreconvenientthanusing<%=...%>.varcompiled=_.template("<%print('Hello'+epithet);%>");compiled({epithet:"stooge"});=>"Hellostooge"IfERB-styledelimitersaren'tyourcupoftea,youcanchangeUnderscore'stemplatesettingstousedifferentsymbolstosetoffinterpolatedcode.Defineaninterpolateregextomatchexpressionsthatshouldbeinterpolatedverbatim,anescaperegextomatchexpressionsthatshouldbeinsertedafterbeingHTML-escaped,andanevaluateregextomatchexpressionsthatshouldbeevaluatedwithoutinsertionintotheresultingstring.Notethatifpartofyourtemplatematchesmorethanoneoftheseregexes,thefirstwillbeappliedbythefollowingorderofpriority:(1)escape,(2)interpolate,(3)evaluate.Youmaydefineoromitanycombinationofthethree.Forexample,toperformMustache.js-styletemplating:_.templateSettings={interpolate:/\{\{(.+?)\}\}/g};vartemplate=_.template("Hello{{name}}!");template({name:"Mustache"});=>"HelloMustache!"Bydefault,templateplacesthevaluesfromyourdatainthelocalscopeviathewithstatement.However,youcanspecifyasinglevariablenamewiththevariablesetting.Thiscansignificantlyimprovethespeedatwhichatemplateisabletorender._.template("Using'with':",{variable:'data'})({answer:'no'});=>"Using'with':no"Precompilingyourtemplatescanbeabighelpwhendebuggingerrorsyoucan'treproduce.Thisisbecauseprecompiledtemplatescanprovidelinenumbersandastacktrace,somethingthatisnotpossiblewhencompilingtemplatesontheclient.Thesourcepropertyisailableonthecompiledtemplatefunctionforeasyprecompilation.<script>JST.project=;</script>Object-OrientedStyleYoucanuseUnderscoreineitheranobject-orientedorafunctionalstyle,dependingonyourpreference.Thefollowingtwolinesofcodeareidenticalwaystodoublealistofnumbers.source,source_.map([1,2,3],function(n){returnn*2;});_([1,2,3]).map(function(n){returnn*2;});ChainingCallingchainwillcauseallfuturemethodcallstoreturnwrappedobjects.Whenyou'vefinishedthecomputation,callvaluetoretrievethefinalvalue.Here'sanexampleofchainingtogetheramap/flatten/reduce,inordertogetthewordcountofeverywordinasong.varlyrics=[{line:1,words:"I'malumberjackandI'mokay"},{line:2,words:"IsleepallnightandIworkallday"},{line:3,words:"He'salumberjackandhe'sokay"},{line:4,words:"Hesleepsallnightandheworksallday"}];_.chain(lyrics).map(function(line){returnline.words.split('');}).flatten().reduce(function(counts,word){counts[word]=(counts[word]||0)+1;returncounts;},{}).value();=>{lumberjack:2,all:4,night:2...}Inaddition,theArrayprototype'smethodsareproxiedthroughthechainedUnderscoreobject,soyoucanslipareverseorapushintoyourchain,andcontinuetomodifythearray.chain_.chain(obj)sourceReturnsawrappedobject.Callingmethodsonthisobjectwillcontinuetoreturnwrappedobjectsuntilvalueiscalled.varstooges=[{name:'curly',e:25},{name:'moe',e:21},{name:'larry',e:23}];varyoungest=_.chain(stooges).sortBy(function(stooge){returnstooge.e;}).map(function(stooge){returnstooge.name+'is'+stooge.e;}).first().value();=>"moeis21"value_.chain(obj).value()sourceExtractsthevalueofawrappedobject._.chain([1,2,3]).reverse().value();=>[3,2,1]Links&SuggestedReadingUnderscore.lua,aLuaportofthefunctionsthatareapplicableinbothlangues.IncludesOOP-wrappingandchaining.(source)Dollar.swift,aSwiftportofmanyoftheUnderscore.jsfunctionsandmore.(source)Underscore.m,anObjective-CportofmanyoftheUnderscore.jsfunctions,usingasyntaxthatencoureschaining.(source)_.m,analternativeObjective-CportthattriestostickalittleclosertotheoriginalUnderscore.jsAPI.(source)Underscore.php,aPHPportofthefunctionsthatareapplicableinbothlangues.TailoredforPHP5.4andmadewithdata-typetoleranceinmind.(source)Underscore-perl,aPerlportofmanyoftheUnderscore.jsfunctions,aimedatonPerlhashesandarrays.(source)Underscore.cfc,aColdfusionportofmanyoftheUnderscore.jsfunctions.(source)Underscore.string,anUnderscoreextensionthataddsfunctionsforstring-manipulation:trim,startsWith,contains,capitalize,reverse,sprintf,andmore.Underscore-ja,ajaportofthefunctionsthatareapplicableinbothlangues.IncludesOOP-wrappingandchaining.(source)Ruby'sEnumerablemodule.Prototype.js,whichprovidesJaScriptwithcollectionfunctionsinthemannerclosesttoRuby'sEnumerable.OliverSteele'sFunctionalJaScript,whichincludescomprehensivehigher-orderfunctionsupportaswellasstringlambdas.MichaelAufreiter'sData.js,adatamanipulation+persistencelibraryforJaScript.Python'sitertools.PyToolz,aPythonportthatextendsitertoolsandfunctoolstoincludemuchoftheUnderscoreAPI.Funcy,apracticalcollectionoffunctionalhelpersforPython,partiallyinspiredbyUnderscore.NotesOntheuseof<inUnderscoreUnderscorefunctionsthatdependonordering,suchas_.sortByand_.sortedIndex,useJaScript’sbuilt-inrelationaloperators,specificallythe“lessthan”operator<.Itisimportanttounderstandthattheseoperatorsareonlymeaningfulfornumbersandstrings.Youcanthrowanyvaluetothem,butJaScriptwillconverttheoperandstostringornumberfirstbeforeperformingtheactualcomparison.Ifyoupassanoperandthatcannotbemeaningfullyconvertedtostringornumber,itendsupbeingNaNbydefault.Thisvalueisunsortable.Ideally,thevaluesthatyouaresortingshouldeitherbeall(meaningfullyconvertibleto)stringsorall(meaningfullyconvertibleto)numbers.Ifthisisnotthecase,youhetwooptions:_.filteroutallunsortablevaluesfirst.Pickatargettype,i.e.,eitherstringornumber,andpassaniterateetoyourUnderscorefunctionthatwillconvertitsargumenttoasensibleinstanceofthetargettype.Forexample,ifyouheanarrayofnumbersthatyouwanttosortandthatmayoccasionallycontainnullorundefined,youcancontrolwhetheryouwanttosortthesebeforeorafterallnumbersbypassinganiterateeto_.sortBythatreturns-Infinityor+Infinityforsuchvalues,respectively.Ormaybeyouwanttotreatthemaszeros;itisuptoyou.ThesameiterateecanalsobepassedtootherUnderscorefunctionstoensurethatthebehiorisconsistent.ChangeLog1.13.7—July24,2024—Diff—DocsFixesabugwhereUnderscoremightthrowanerroronloadiftheexecutingenvironmentoverridesthenativeDatiew.Addsadirectlinktothecorrespondingsourcecodeforeachfunctioninthedocumentation.Clarifiesthedocumentationforthestepargumentoftherangefunction.Addsdarkmodesupporttothehomepe.Otherinfrastructuralimprovementstofunding,testingandbuilding.1.13.6—September24,2022—Diff—DocsHotfixforversion1.13.5toremoveapostinstallscriptfromthepacke.json,whichunexpectedlybrokemanypeople'sbuilds.1.13.5—September23,2022—Diff—DocsAddsamodulesub-entrytothepacke.json’sexports.requirecondition.Whenabundlingtool,suchasRollupwithrecentversionsof@rollup/plugin-node-resolve,takestheexportsmapveryliterally,thisshouldpreventsituationsinwhichthefinalbundleincludesmultiplecopiesofUnderscoreindifferentmoduleformats.Updatestothetestinginfrastructureanddevelopmentdependencies.Nocodechanges.1.13.4—June2,2022—Diff—DocsFixesacompatibilityissuewithWebPackmodulefederation.Documentationimprovements.1.13.3—April23,2022—Diff—DocsFixesacompatibilityissuewithExtendScript.Variousimprovementstotestingandcontinuousintegration,includingtheadditionofsecurityscanningandareducedcarbonfootprint.1.13.2—December16,2021—Diff—DocsFixesaregressionintroducedin1.9.0thatcaused_.sampleand_.shuffletonolongerworkonstrings.FixesanissueinIE8compatibilitycode.Makesthewebsitemobile-friendly.Variousotherminordocumentationenhancementsandanewtest.1.13.1—April15,2021—Diff—DocsRestorestheunderscore.jsaliascommittedtotheGitHubrepository.Addssomebuildclarificationstothedocumentation.Nocodechanges.1.13.0—April9,2021—Diff—DocsMergesthechangesfromthe1.13.0-0through1.13.0-3previewreleasesintothemainreleasestreamfollowingversion1.12.1.Asofthisrelease,ESMsupportis100%.Addsasecuritypolicytothedocumentation.Addsfundinginformationtothedocumentation.1.13.0-3—March31,2021—Diff—DocsAddsa"module"exportsconditiontothepacke.json,whichshouldtheoreticallyhelptooidduplicatecodebundlingwithexports-awarebuildtools.Re-synchronizessomecommentsanddocumentationtextwiththe1.12.xbranch.1.13.0-2—March15,2021—Diff—DocsFixesthesamesecurityissuein_.templateastheparallel1.12.1release.1.12.1—March15,2021—Diff—DocsFixesasecurityissuein_.templatethatcouldenableathirdpartytoinjectcodeincompiledtemplates.ThisissueaffectsallversionsofUnderscorebetween1.3.2and1.12.0,inclusive,aswellaspreviewreleases1.13.0-0and1.13.0-1.Thefixinthisreleaseisalsoincludedintheparallelpreviewrelease1.13.0-2.CVE-Restoresanoptimizationin_.debouncethatwasunintentionallylostinversion1.9.0(sameasinparallelpreviewrelease1.13.0-0).Varioustestanddocumentationenhancements(sameasinparallelpreviewreleases1.13.0-0and1.13.0-1).1.13.0-1—March11,2021—Diff—DocsFixesanissuethatcausedaliasestobeabsentamongthenamedexportsinthenewnativeESMentrypointforNode.js12+.Moretestanddocumentationfixesandenhancements.1.13.0-0—March9,2021—Diff—DocsAddsexperimentalsupportfornativeESMimportsinNode.js.YoucannowalsodonamedimportsorevendeepmoduleimportsdirectlyfromaNode.jsprocessinNode.jsversion12andlater.Monolithicimportsarerecommendedforuseinproduction.State(suchasmixed-infunctions)issharedbetweenCommonJSandESMconsumers.RenamestheUMDbundletounderscore-umd.jsforconsistencywiththeotherbundlenames.Analiasnamedunderscore.jsisretainedforbackwardscompatibility.Restoresanoptimizationin_.debouncethatwasunintentionallylostinversion1.9.0.Varioustestanddocumentationenhancements.1.12.0—November24,2020—Diff—DocsAddsthe_.getand_.toPathfunctions.ThelattercanbeoverriddeninordertocustomizetheinterpretationofdeeppropertypathsthroughoutUnderscore.AfutureversionofUnderscore-contribwillbeprovidingaready-madefunctionforthispurpose;userswillbeabletooptintostring-basedpathshorthandssuchas'a.0.b'and'a[0]["b"]'byusingthatfunctionfromUnderscore-contribtooverride_.toPath.Fixesabugin_.isEqualthatcausedtypedarraystocompareequalwhenviewingdifferentsegmentsofthesameunderlyingArrayBuffer.Improvesthecompatibilityof_.isEqual,_.isDatiew,_.isMap,_.isWeakMapand_.isSetwithsomeolderbrowsers,especiallyIE11.Significantlyenhancestheperformanceof_.isEmptyandseveralmembersoftheisTypefamilyoffunctions.Speedsup_.isEqualcomparisonoftypedarraysandDatiewswithidentialbuffer,byteOffsetandbyteLength.Restorescross-browsertestingduringcontinuousintegrationtoitsformergloryandaddsdocumentationaboutenginecompatibility.Slimsdownthedevelopmentdependenciesfortesting.1.11.0—August28,2020—Diff—Docs—ArticlePutsthesourceofeveryfunctioninaseparatemodule,followinguponthemovetoEcmaScript6exportnotationinversion1.10.0.AMDandCommonJSversionsofthefunctionmodulesareprovidedaswell.ThisbringsperfecttreeshakingtoallusersandunlocksthepossibilitytocreatearbitrarycustomUnderscorebuildswithoutcodesizeoverhead.modules/index.jsisstillpresentandtheUMDbundleisstillrecommendedformostusers.Sincethemodularizationobfuscatesthediff,piecewisediffsareprovidedbelow.ChangesbeforemodularizationModularizationitselfChangesaftermodularizationAddsamonolithicbundleinEcmaScript6moduleformat,underscore-esm.js,asamodernalternativetothemonolithicUMDbundle.UserswhowanttouseESmoduleimportsinthebrowserareadvisedtousethisnewbundleinsteadofmodules/index.js,becauseunderscore-esm.jsprovidesthecompleteUnderscoreinterfaceinasingledownload.Addsamodularversionoftheannotatedsource,reflectingthefullinternalstructureoftheprimarysourcecode.Adds_.isArrayBuffer,_.isDatiewand_.isTypedArrayfunctions,aswellassupportforthecorrespondingvaluetypesto_.isEqual.Addstheoptiontoflattenarraystoaspecificdepth:_.flatten(anArray,3).Adds_.transposeasanaliasto_.unzip.FixesaninconsistencywhereArray.prototypemethodsontheUnderscorewrapperwoulderrorwhenthewrappedvalueisnullorundefined.Thesemethodsnowperformano-oponnullvaluesliketheotherUnderscorefunctions.Fixesabugthatcaused_.firstand_.lasttoreturn[]insteadofundefinedforemptyarrayswhenusedasaniteratee.Fixesaregressionintroducedinversion1.9.0thatcaused_.bindAlltoreturnundefinedinsteadoftheboundobject.RestorescontinuousintegrationtestingwithTrisCI.Replacesstigmatizing“whitelist”/“blacklist”terminologyincommentsanddocumentationbyneutral“allowed”/“disallowed”terminology.Variousclarificationsandminorenhancementsandfixestothedocumentation,sourcecommentsandatest.1.10.2—March30,2020—Diff—DocsFixesabugintroducedwith1.10.0,whileusingthelegacyNode.jsrequireAPI:var_=require("underscore")._1.10.1—March30,2020—Diff—DocsFixedrelativelinksamongtheESModulestoincludethefileextension,forwebbrowsersupport.1.10.0—March30,2020—Diff—DocsReformatsthesourcecodetouseEcmaScript6exportnotation.Theunderscore.jsUMDbundleisnowcompiledfromunderlyingsourcemodulesinsteadofbeingthesource.Fromnowon,Rollupusershetheoptiontoimportfromtheunderlyingsourcemoduleinordertoenabletreeshaking.Explicitlystatesinthedocumentation,andverifiesintheunittests,that_.sortedIndex(array,value)alwaysreturnsthelowerbound,i.e.,thesmallestindex,atwhichvaluemaybeinsertedinarray.Makesthenotationofthe_.maxunittestconsistentwithotherunittests.Fixesabugthatwouldcauseinfiniterecursionifanoverriddenimplementationof_.iterateeattemptedtofallbacktotheoriginalimplementation.RestorescompatibilitywithEcmaScript3andExtendScript.1.9.2—Jan6,2020—Diff—DocsNocodechanges.UpdatedatesttohelpoutCITGM.1.9.1—May31,2018—Diff—DocsFixesedge-caseregressionsfrom1.9.0,includingcertainformsofcalling_.firstand_.lastonanemptyarray,andpassingarraysaskeysto_.countByand_.groupBy.1.9.0—April18,2018—Diff—DocsAddsthe_.restArgumentsfunctionforvariadicfunctionhandling.Addsthe_.chunkfunctionforchunkingupanarray.Addsa_.isSymbol,_.isMap,_.isWeakMap,_.isSetand_.isWeakSetfunctions._.throttleand_.debouncereturnfunctionsthatnowhea.cancel()method,whichcanbeusedtocancelanyscheduledcalls._.propertynowacceptsarraysofkeysandindexesaspathspecifiers,forlookingupadeeppropertiesofavalue._.rangenowacceptsnegativerangestogeneratedescendingarrays.Addssupportforseveralenvironmentsincluding:WebWorkers,browserifyandES6imports.Removesthecomponent.jsonastheComponentpackemanementsystemisdiscontinued.Theplaceholderusedforpartialisnowconfigurablebysetting_.partial.placeholder._.bindAllnowacceptsarraysorargumentsforkeys.Threeyearsofperformanceimprovements.1.8.3—April2,2015—Diff—DocsAddsan_.createmethod,asaslimmeddownversionofObject.create.WorksaroundaniOSbugthatcanimproperlycauseisArrayLiketobeJIT-ed.Alsofixesabugwhenpassing0toisArrayLike.1.8.2—Feb.22,2015—Diff—DocsRestoresthepreviousold-Internet-Exploreredgecaseschangedin1.8.1.AddsafromIndexargumentto_.contains.1.8.1—Feb.19,2015—Diff—DocsFixes/changessomeold-InternetExplorerandrelatededgecasebehior.TestyourappwithUnderscore1.8.1inanoldIEandletusknowhowit'sdoing...1.8.0—Feb.19,2015—Diff—DocsAdded_.mapObject,whichissimilarto_.map,butjustforthevaluesinyourobject.(Arealcrowdpleaser.)Added_.allKeyswhichreturnsalltheenumerablepropertynamesonanobject.Reverteda1.7.0changewhere_.extendonlycopied"own"properties.Hopefullythiswillun-breakyou—ifitbreaksyouain,Iapologize.Added_.extendOwn—aless-usefulformof_.extendthatonlycopiesover"own"properties.Added_.findIndexand_.findLastIndexfunctions,whichnicelycomplementtheirtwin-twins_.indexOfand_.lastIndexOf.Addedan_.isMatchpredicatefunctionthattellsyouifanobjectmatcheskey-valueproperties.Akissingcousinof_.isEqualand_.matcher.Addedan_.isErrorfunction.Restoredthe_.unzipfunctionastheinverseofzip.Flip-flopping.Iknow._.resultnowtakesanoptionalfallbackvalue(orfunctionthatprovidesthefallbackvalue).Addedthe_.propertyOffunctiongeneratorasamirror-worldversionof_.property.Deprecated_.matches.It'snowknownbyamoreharmoniousname—_.matcher.Variousanddiversecodesimplifications,changesforimprovedcross-platformcompatibility,andedgecasebugfixes.1.7.0—August26,2014—Diff—DocsForconsistencyandspeedacrossbrowsers,UnderscorenowignoresnativearraymethodsforforEach,map,reduce,reduceRight,filter,every,some,indexOf,andlastIndexOf."Sparse"arraysareofficiallydeadinUnderscore.Added_.iterateetocustomizetheiteratorsusedbycollectionfunctions.ManyUnderscoremethodswilltakeastringargumentforeasier_.property-stylelookups,anobjectfor_.where-stylefiltering,orafunctionasacustomcallback.Added_.beforeasacounterpartto_.after.Added_.negatetoinvertthetruthvalueofapassed-inpredicate.Added_.noopasahandyemptyplaceholderfunction._.isEmptynowworkswithargumentsobjects._.hasnowguardsainstnullishobjects._.omitcannowtakeaniterateefunction._.partitionisnowcalledwithindexandobject._.matchescreatesashallowcloneofyourobjectandonlyiteratesoverownproperties.AligningbetterwiththeforthcomingECMA6Object.assign,_.extendonlyiteratesovertheobject'sownproperties.Falsyguardsarenolongerneededin_.extendand_.defaults—ifthepassedinargumentisn'taJaScriptobjectit'sjustreturned.Fixedafewedgecasesin_.maxand_.mintohandlearrayscontainingNaN(likestringsorotherobjects)andInfinityand-Infinity.Overridebasemethodslikeeachandsomeandthey'llbeusedinternallybyotherUnderscorefunctionstoo.Theescapefunctionshandlebackticks(`),todealwithanIE≤8bug.Forconsistency,_.unionand_.differencenowonlyworkwitharraysandnotvariadicargs._.memoizeexposesthecacheofmemoizedvaluesasapropertyonthereturnedfunction._.pickacceptsiterateeandcontextargumentsforamoreadvancedcallback.Underscoretemplatesnolongeracceptaninitialdataobject._.templatealwaysreturnsafunctionnow.Optimizationsandcodecleanupaplenty.1.6.0—February10,2014—Diff—DocsUnderscorenowregistersitselfforAMD(Require.js),BowerandComponent,aswellasbeingaCommonJSmoduleandaregular(Ja)Script.Anugliness,butperhapsanecessaryone.Added_.partition,awaytosplitacollectionintotwolistsofresults—thosethatpassandthosethatfailaparticularpredicate.Added_.property,foreasycreationofiteratorsthatpullspecificpropertiesfromobjects.UsefulinconjunctionwithotherUnderscorecollectionfunctions.Added_.matches,afunctionthatwillgiveyouapredicatethatcanbeusedtotellifagivenobjectmatchesalistofspecifiedkey/valueproperties.Added_.constant,asahigher-order_.identity.Added_.now,anoptimizedwaytogetatimestamp—usedinternallytospeedupdebounceandthrottle.The_.partialfunctionmaynowbeusedtopartiallyapplyanyofitsarguments,bypassing_whereveryou'dlikeaplaceholdervariable,tobefilled-inlater.The_.eachfunctionnowreturnsareferencetothelistforchaining.The_.keysfunctionnowreturnsanemptyarrayfornon-objectsinsteadofthrowing.…andmoremiscellaneousrefactoring.1.5.2—September7,2013—Diff—DocsAddedanindexByfunction,whichfitsinalongsideitscousins,countByandgroupBy.Addedasamplefunction,forsamplingrandomelementsfromarrays.Someoptimizationsrelatingtofunctionsthatcanbeimplementedintermsof_.keys(whichincludes,significantly,eachonobjects).Alsofordebounceinatightloop.The_.escapefunctionnolongerescapes'/'.1.5.1—July8,2013—Diff—DocsRemovedunzip,asit'ssimplytheapplicationofziptoanarrayofarguments.Use_.zip.apply(_,list)totransposeinstead.1.5.0—July6,2013—Diff—DocsAddedanewunzipfunction,astheinverseof_.zip.Thethrottlefunctionnowtakesanoptionsargument,allowingyoutodisableexecutionofthethrottledfunctiononeithertheleadingortrailingedge.AsourcemapisnowsuppliedforeasierdebuggingoftheminifiedproductionbuildofUnderscore.Thedefaultsfunctionnowonlyoverridesundefinedvalues,notnullones.Removedtheabilitytocall_.bindAllwithnomethodnamearguments.It'sprettymuchalwayswisertoallowthenamesofthemethodsyou'dliketobind.Removedtheabilitytocall_.afterwithaninvocationcountofzero.Theminimumnumberofcallsis(naturally)now1.1.4.4—January30,2013—Diff—DocsAdded_.findWhere,forfindingthefirstelementinalistthatmatchesaparticularsetofkeysandvalues.Added_.partial,forpartiallyapplyingafunctionwithoutchangingitsdynamicreferencetothis.Simplifiedbindbyremovingsomeedgecasesinvolvingconstructorfunctions.Inshort:don't_.bindyourconstructors.Aminoroptimizationtoinvoke.Fixbugintheminifiedversionduetotheminifierincorrectlyoptimizing-awayisFunction.1.4.3—December4,2012—Diff—DocsImprovedUnderscorecompatibilitywithAdobe'sJSenginethatcanbeusedtoscriptIllustrator,Photoshop,andfriends.Addedadefault_.identityiteratortocountByandgroupBy.Theuniqfunctioncannowtakearray,iterator,contextastheargumentlist.Thetimesfunctionnowreturnsthemappedarrayofiteratorresults.Simplifiedandfixedbugsinthrottle.1.4.2—October6,2012—Diff—DocsForbackwardscompatibility,returnedtopre-1.4.0behiorwhenpassingnulltoiterationfunctions.Theynowbecomeno-opsain.1.4.1—October1,2012—Diff—DocsFixeda1.4.0regressioninthelastIndexOffunction.1.4.0—September27,2012—Diff—DocsAddedapairsfunction,forturningaJaScriptobjectinto[key,value]pairs...aswellasanobjectfunction,forconvertinganarrayof[key,value]pairsintoanobject.AddedacountByfunction,forcountingthenumberofobjectsinalistthatmatchacertaincriteria.Addedaninvertfunction,forperformingasimpleinversionofthekeysandvaluesinanobject.Addedawherefunction,foreasycasesoffilteringalistforobjectswithspecificvalues.Addedanomitfunction,forfilteringanobjecttoremovecertainkeys.Addedarandomfunction,toreturnarandomnumberinagivenrange._.debounce'dfunctionsnowreturntheirlastupdatedvalue,justlike_.throttle'dfunctionsdo.ThesortByfunctionnowrunsastablesortalgorithm.AddedtheoptionalfromIndexoptiontoindexOfandlastIndexOf."Sparse"arraysarenolongersupportedinUnderscoreiterationfunctions.Useaforloopinstead(orbetteryet,anobject).Theminandmaxfunctionsmaynowbecalledonverylargearrays.Interpolationintemplatesnowrepresentsnullandundefinedastheemptystring.Underscoreiterationfunctionsnolongeracceptnullvaluesasano-opargument.You'llgetanearlyerrorinstead.Anumberofedge-casesfixesandtweaks,whichyoucanspotinthediff.Dependingonhowyou'reusingUnderscore,1.4.0maybemorebackwards-incompatiblethanusual—pleasetestwhenyouupgrade.1.3.3—April10,2012—Diff—DocsManyimprovementsto_.template,whichnowprovidesthesourceofthetemplatefunctionasaproperty,forpotentiallyevenmoreefficientpre-compilationontheserver-side.Youmaynowalsosetthevariableoptionwhencreatingatemplate,whichwillcauseyourpassed-indatatobemadeailableunderthevariableyounamed,insteadofusingawithstatement—significantlyimprovingthespeedofrenderingthetemplate.Addedthepickfunction,whichallowsyoutofilteranobjectliteralwithalistofallowedpropertynames.AddedtheresultfunctioUnderscore. jsn,forconveniencewhenworkingwithAPIsthatalloweitherfunctionsorrawproperties.AddedtheisFinitefunction,becausesometimesknowingthatavalueisanumberjustain'tquiteenough.ThesortByfunctionmaynowalsobepassedthestringnameofapropertytouseasthesortorderoneachobject.Fixeduniqtoworkwithsparsearrays.Thedifferencefunctionnowperformsashallowflatteninsteadofadeeponewhencomputingarraydifferences.Thedebouncefunctionnowtakesanimmediateparameter,whichwillcausethecallbacktofireontheleadinginsteadofthetrailingedge.1.3.1—January23,2012—Diff—DocsAddedan_.hasfunction,asasaferwaytousehasOwnProperty.Added_.collectasanaliasfor_.map.Smalltalkers,rejoice.Revertedanoldchangesothat_.extendwillcorrectlycopyoverkeyswithundefinedvaluesain.Bugfixtostopescapingslasheswithininterpolationsin_.template.1.3.0—January11,2012—Diff—DocsRemovedAMD(RequireJS)supportfromUnderscore.Ifyou'dliketouseUnderscorewithRequireJS,youcanloaditasanormalscript,wraporpatchyourcopy,ordownloadaforkedversion.1.2.4—January4,2012—Diff—DocsYounowcan(andprobablyshould,asit'ssimpler)write_.chain(list)insteadof_(list).chain().FixforescapedcharactersinUnderscoretemplates,andforsupportingcustomizationsof_.templateSettingsthatonlydefineoneortwooftherequiredregexes.Fixforpassinganarrayasthefirstargumenttoan_.wrap'dfunction.ImprovedcompatibilitywithClojureScript,whichaddsacallfunctiontoString.prototype.1.2.3—December7,2011—Diff—DocsDynamicscopeisnowpreservedforcompiled_.templatefunctions,soyoucanusethevalueofthisifyoulike.Sparsearraysupportof_.indexOf,_.lastIndexOf.Both_.reduceand_.reduceRightcannowbepassedanexplicitlyundefinedvalue.(There'snoreasonwhyyou'dwanttodothis.)1.2.2—November14,2011—Diff—DocsContinuedtweaksto_.isEqualsemantics.NowJSprimitivesareconsideredequivalenttotheirwrappedversions,andarraysarecomparedbytheirnumericpropertiesonly(#351)._.escapenolongertriestobesmartaboutnotdouble-escapingalready-escapedHTMLentities.Nowitjustescapesregardless(#350).In_.template,youmaynowleesemicolonsoutofevaluatedstatementsifyouwish:<%})%>(#369)._.after(callback,0)willnowtriggerthecallbackimmediately,making"after"easiertousewithasynchronousAPIs(#366).1.2.1—October24,2011—Diff—DocsSeveralimportantbugfixesfor_.isEqual,whichshouldnowdobetteronmutatedArrays,andonnon-Arrayobjectswithlengthproperties.(#329)JamesBurkecontributedUnderscoreexportingforAMDmoduleloaders,andTonyLukaseforAppceleratorTitanium.(#335,#338)Youcannow_.groupBy(list,'property')asashortcutforgroupingvaluesbyaparticularcommonproperty._.throttle'dfunctionsnowfireimmediatelyuponinvocation,andarerate-limitedthereafter(#170,#266).Mostofthe_.is[Type]checksnolongerducktype.The_.bindfunctionnowalsoworksonconstructors,a-laES5...butyouwouldneverwanttouse_.bindonaconstructorfunction._.clonenolongerwrapsnon-objecttypesinObjects._.findand_.filterarenowthepreferrednamesfor_.detectand_.select.1.2.0—October5,2011—Diff—DocsThe_.isEqualfunctionnowsupportstruedeepequalitycomparisons,withchecksforcyclicstructures,thankstoKitCambridge.UnderscoretemplatesnowsupportHTMLescapinginterpolations,using<%-...%>syntax.RyanTenneycontributed_.shuffle,whichusesamodifiedFisher-Yatestogiveyouashuffledcopyofanarray._.uniqcannowbepassedanoptionaliterator,todeterminebywhatcriteriaanobjectshouldbeconsideredunique._.lastnowtakesanoptionalargumentwhichwillreturnthelastNelemUnderscore. jsentsofthelist.Anew_.initialfunctionwasadded,asamirrorof_.rest,whichreturnsalltheinitialvaluesofalist(exceptthelastN).1.1.7—July13,2011—Diff—DocsAdded_.groupBy,whichgregatesacollectionintogroupsoflikeitems.Added_.unionand_.difference,tocomplementthe(re-named)_.intersection.Variousimprovementsforsupportofsparsearrays._.toArraynowreturnsaclone,ifdirectlypassedanarray._.functionsnowalsoreturnsthenamesoffunctionsthatarepresentintheprototypechain.1.1.6—April18,2011—Diff—DocsAdded_.after,whichwillreturnafunctionthatonlyrunsafterfirstbeingcalledaspecifiednumberoftimes._.invokecannowtakeadirectfunctionreference._.everynowrequiresaniteratorfunctiontobepassed,whichmirrorstheES5API._.extendnolongercopieskeyswhenthevalueisundefined._.bindnowerrorswhentryingtobindanundefinedvalue.1.1.5—March20,2011—Diff—DocsAddedan_.defaultsfunction,forusemergingtogetherJSobjectsrepresentingdefaultoptions.Addedan_.oncefunction,formanufacturingfunctionsthatshouldonlyeverexecuteasingletime._.bindnowdelegatestothenativeES5version,whereailable._.keysnowthrowsanerrorwhenusedonnon-Objectvalues,asinES5.Fixedabugwith_.keyswhenusedoversparsearrays.1.1.4—January9,2011—Diff—DocsImprovedcompliancewithES5'sArraymethodswhenpassingnullasavalue._.wrapnowcorrectlysetsthisforthewrappedfunction._.indexOfnowtakesanoptionalflforfindingtheinsertionindexinanarraythatisguaranteedtoalreadybesorted.oidingtheuseof.callee,toallow_.isArraytoworkproperlyinES5'sstrictmode.1.1.3—December1,2010—Diff—DocsInCommonJS,Underscoremaynowberequiredwithjust:var_=require("underscore").Added_.throttleand_.debouncefunctions.Removed_.breakLoop,inforofanES5-styleun-break-ableeachimplementation—thisremovesthetry/catch,andyou'llnowhebetterstacktracesforexceptionsthatarethrownwithinanUnderscoreiterator.ImprovedtheisTypefamilyoffunctionsforbetterinteroperabilitywithInternetExplorerhostobjects._.templatenowcorrectlyescapesbackslashesintemplates.Improved_.reducecompatibilitywiththeES5version:ifyoudon'tpassaninitialvalue,thefirstiteminthecollectionisused._.eachnolongerreturnstheiteratedcollection,forimprovedconsistencywithES5'sforEach.1.1.2—October15,2010—Diff—DocsFixed_.contains,whichwasmistakenlypointingat_.intersectinsteadof_.include,likeitshouldhebeen.Added_.uniqueasanaliasfor_.uniq.1.1.1—October5,2010—Diff—DocsImprovedthespeedof_.template,anditshandlingofmultilineinterpolations.RyanTenneycontributedoptimizationstomanyUnderscorefunctions.Anannotatedversionofthesourcecodeisnowailable.1.1.0—August18,2010—Diff—DocsThemethodsignatureof_.reducehasbeenchangedtomatchtheES5signature,insteadoftheRuby/Prototype.jsversion.Thisisabackwards-incompatiblechange._.templatemaynowbecalledwithnoarguments,andpreserveswhitespace._.containsisanewaliasfor_.include.1.0.4—June22,2010—Diff—DocsAndriMöllcontributedthe_.memoizefunction,whichcanbeusedtospeedupexpensiverepeatedcomputationsbycachingtheresults.1.0.3—June14,2010—Diff—DocsPatchthatmakes_.isEqualreturnfalseifanypropertyofthecomparedobjecthasaNaNvalue.Technicallythecorrectthingtodo,butofquestionablesemantics.WatchoutforNaNcomparisons.1.0.2—March23,2010—Diff—DocsFixes_.isArgumentsinrecentversionsofOpera,whichheargumentsobjectsasrealArrays.1.0.1—March19,2010—Diff—DocsBugfixfor_.isEqual,whencomparingtwoobjectswiththesamenumberofundefinedkeys,butwithdifferentnames.1.0.0—March18,2010—Diff—DocsThingshebeenstableformanymonthsnow,soUnderscoreisnowconsideredtobeoutofbeta,at1.0.Improvementssince0.6include_.isBoolean,andtheabilitytohe_.extendtakemultiplesourceobjects.0.6.0—February24,2010—Diff—DocsMajorrelease.IncorporatesanumberofMileFrawley'srefactorsforsaferduck-typingoncollectionfunctions,andcleanerinternals.Anew_.mixinmethodthatallowsyoutoextendUnderscorewithutilityfunctionsofyourown.Added_.times,whichworksthesameasinRubyorPrototype.js.NativesupportforES5'sArray.isArray,andObject.keys.0.5.8—January28,2010—Diff—DocsFixedUnderscore'scollectionfunctionstoworkonNodeListsandHTMLCollectionsoncemore,thankstoJustinTulloss.0.5.7—January20,2010—Diff—DocsAsaferimplementationof_.isArguments,andafaster_.isNumber,thankstoJedSchmidt.0.5.6—January18,2010—Diff—DocsCustomizabledelimitersfor_.template,contributedbyNoahSloan.0.5.5—January9,2010—Diff—DocsFixforabuginMobileSafari'sOOP-wrapper,withtheargumentsobject.0.5.4—January5,2010—Diff—DocsFixformultiplesinglequoteswithinatemplatestringfor_.template.See:RickStrahl'sblogpost.0.5.2—January1,2010—Diff—DocsNewimplementationsofisArray,isDate,isFunction,isNumber,isRegExp,andisString,thankstoasuggestionfromRobertKieffer.InsteadofdoingObject#toStringcomparisons,theynowcheckforexpectedproperties,whichislesssafe,butmorethananorderofmnitudefaster.MostotherUnderscorefunctionssawminorspeedimprovementsasaresult.EvgeniyDolzhenkocontributed_.tap,similartoRuby1.9's,whichishandyforinjectingsideeffects(likelogging)intochainedcalls.0.5.1—December9,2009—Diff—DocsAddedan_.isArgumentsfunction.LotsoflittlesafetychecksandoptimizationscontributedbyNoahSloanandAndriMöll.0.5.0—December7,2009—Diff—Docs[APIChanges]_.bindAllnowtakesthecontextobjectasitsfirstparameter.Ifnomethodnamesarepassed,allofthecontextobject'smethodsareboundtoit,enablingchainingandeasierbinding._.functionsnowtakesasingleargumentandreturnsthenamesofitsFunctionproperties.Calling_.functions(_)willgetyouthepreviousbehior.Added_.isRegExpsothatisEqualcannowtestforRegExpequality.Allofthe"is"functionshebeenshrunkdownintoasingledefinition.KarlGuertincontributedpatches.0.4.7—December6,2009—Diff—DocsAddedisDate,isNaN,andisNull,forcompleteness.OptimizationsforisEqualwhencheckingequalitybetweenArraysorDates._.keysisnow25%–2Xfaster(dependingonyourbrowser)whichspeedsupthefunctionsthatrelyonit,suchas_.each.0.4.6—November30,2009—Diff—DocsAddedtherangefunction,aportofthePythonfunctionofthesamename,forgeneratingflexibly-numberedlistsofintegers.OriginalpatchcontributedbyKirillIshanov.0.4.5—November19,2009—Diff—DocsAddedrestforArraysandargumentsobjects,andaliasedfirstashead,andrestastail,thankstoLukeSutton'spatches.AddedtestsensuringthatallUnderscoreArrayfunctionsalsoworkonargumentsobjects.0.4.4—November18,2009—Diff—DocsAddedisString,andisNumber,forconsistency.Fixed_.isEqual(NaN,NaN)toreturntrue(whichisdebatable).0.4.3—November9,2009—Diff—DocsStartedusingthenativeStopIterationobjectinbrowsersthatsupportit.FixedUnderscoresetupforCommonJSenvironments.0.4.2—November9,2009—Diff—DocsRenamedtheunwrappingfunctiontovalue,forclarity.0.4.1—November8,2009—Diff—DocsChainedUnderscoreobjectsnowsupporttheArrayprototypemethods,sothatyoucanperformthefullrangeofoperationsonawrappedarraywithouthingtobreakyourchain.AddedabreakLoopmethodtobreakinthemiddleofanyUnderscoreiteration.AddedanisEmptyfunctionthatworksonarraysandobjects.0.4.0—November7,2009—Diff—DocsAllUnderscorefunctionscannowbecalledinanobject-orientedstyle,likeso:_([1,2,3]).map(...);.OriginalpatchprovidedbyMarc-AndréCournoyer.Wrappedobjectscanbechainedthroughmultiplemethodinvocations.Afunctionsmethodwasadded,providingasortedlistofallthefunctionsinUnderscore.0.3.3—October31,2009—Diff—DocsAddedtheJaScript1.8functionreduceRight.Aliaseditasfoldr,andaliasedreduceasfoldl.0.3.2—October29,2009—Diff—DocsNowrunsonstockRhinointerpreterswith:load("underscore.js").Addedidentityasautilityfunction.0.3.1—October29,2009—Diff—DocsAlliteratorsarenowpassedintheoriginalcollectionastheirthirdargument,thesameasJaScript1.6'sforEach.Iteratingoverobjectsisnowcalledwith(value,key,collection),fordetailssee_.each.0.3.0—October29,2009—Diff—DocsAddedDmitryBaranovskiy'scomprehensiveoptimizations,mergedinKrisKowal'spatchestomakeUnderscoreCommonJSandNarwhalcompliant.0.2.0—October28,2009—Diff—DocsAddedcomposeandlastIndexOf,renamedinjecttoreduce,addedaliasesforinject,filter,every,some,andforEach.0.1.1—October28,2009—Diff—DocsAddednoConflict,sothatthe"Underscore"objectcanbeassignedtoothervariables.0.1.0—October28,2009—DocsInitialreleaseofUnderscore.js.varmyN=document.getElementById("myN")varmenu=document.getElementById('menu')myN.addEventListener('click',(e)=>{if(e.target.tName=="A"){menu.checked=true}})

Seite? ˅:Underscore. jsBericht

Wenn ein Verstoß gegen die Website vorliegt, klicken Sie bitte auf MeldenBericht

Empfohlene Informationen

Empfohlene Seite