From the course: Learning Functional Programming with JavaScript (ES5)

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

Combining functions

Combining functions - JavaScript Tutorial

From the course: Learning Functional Programming with JavaScript (ES5)

Start my 1-month free trial

Combining functions

- [Instructor] We've seen that the functions map, filter, and reduce can be very useful on their own. But combining these functions can make them even more useful. Let's go through an example to see how. Imagine that we have an array of employee data. We want to find out how the average age of males compares with that of females. By chaining the map, filter, and reduce functions together we can neatly perform this calculation. The first thing we'll do is filter our array so that we end up with an array of only the male employees. To do this we call filter with a function that checks if each employee is male and returns true if they are. This will give us an array of all the male employees from our original array. We then want to convert this array of male employees into an array of the ages of the male employees. To do this we use map with a function that takes a male employee object and changes it into a number representing his age. Now that we have the ages of all the male employees…

Contents