Underscore methods

Collections, such as Models, have a number of methods inherited from Underscore, but Collections actually have a lot more methods than Models...28 methods to be exact. We can't possibly cover all these methods in detail here, so I'll just provide a quick summary of what each method does and then explore a few of the more important ones in depth.

Note that just as with the Underscore methods on Model, some of these methods operate on the attributes of the Models inside the Collection rather than on the Collection or Models themselves. At the same time, however, all the methods that return multiple results return plain old JavaScript arrays, not new Collections. This is done for performance reasons and shouldn't be an issue because if you need those results as a Collection, you can simply create a new one and pass in the results array.

Name

What it does

each

This iterates over every Model in the Collection

map

This returns an array of values by transforming every Model in the Collection

reduce

This returns a single value generated from all the Models in a Collection

reduceRight

The same as reduc e, except that it iterates backwards

find

This returns the first Model that matches a testing function

filter

This returns all the Models that match a testing function

reject

This returns all the Models that don't match a testing function

every

This returns true if every Model in the Collection matches a test function

some

This returns true if some (any) Model in the Collection matches a test function

contains

This returns true if the Collection includes the provided Model

invoke

This calls a specified function on every Model in the Collection and returns the result

max

This applies a conversion function to every Model in the Collection and returns the maximum value returned

min

This applies a conversion function to every Model in the Collection and returns the minimum value returned

sortBy

This returns the Collection's Models sorted based on an indicated attribute

groupBy

This returns the Collection's Models grouped by an indicated attribute

shuffle

This returns one or more randomly chosen Models from the Collection

toArray

This returns an array of the Collection's Models

size

This returns a count of Models in the Collection, such as Collection.length

first

This returns the first (or first N) Model(s) in the Collection

initial

This returns all but the last Model in the Collection

rest

This returns all the Models in the Collection after the first N

last

This returns the last (or last N) Model(s) in the Collection

without

This returns all the Models in the Collection except the provided ones

indexOf

This returns the index of the first provided Model in the Collection

lastIndexOf

This returns the index of the last provided Model in the Collection

isEmpty

This returns true if the Collection contains no Models

chain

This returns a version of Collection that can have multiple Underscore methods called on it successively (chained); call the value method at the end of the chain to get the result of the calls

pluck

This returns the provided attribute from each Model in the Collection

where

This returns all the Models in the Collection that match the provided attribute template

findWhere

This returns the first Model found in the Collection that matches the provided attribute template

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.44.121