Book Image

Learning Underscore.js

By : Alexandru Vasile Pop
Book Image

Learning Underscore.js

By: Alexandru Vasile Pop

Overview of this book

Table of Contents (14 chapters)
Learning Underscore.js
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Aggregations and transformations


We dealt so far with functions tasked with extracting a value or a range of values from collections, and we will now move to functions that aggregate or transform collections. Aggregate functions operate over an entire collection to extract or calculate a single value or a limited set of values. Transformation functions operate over an entire collection and return a new collection that is usually a different representation of the original collection.

The first example of an aggregate function was _.reduce() and the first example of a transformation function was _.map(). The former calculates an accumulator value from the entire collection and the latter creates a new collection from the original collection.

Aggregations

We will use our example data for this chapter to revisit _.reduce() and define a new requirement to calculate the average rental price for all bicycles or for bicycles of a specific type. We will implement the getAverageRentalPrice(type) function...