That’s the sound of HOF working on the Chain Gang

Edward
4 min readSep 23, 2020
Photo by Stephen Leonardi on Unsplash

Chaining functions can be a very helpful in working with dynamic data sets. With the help of a few crucial high order functions, you can manipulate, grab, or single out the exact bit of information you want with a single line of code. Don’t believe me? Well… Let’s make a believer out of you below:

First things first!

Let me introduce you to some of the high order functions we are going to be working with in todays lesson.

.reduce

This method takes an array or object and “reduces” it to a single value. Think of it as a massive Rube Goldberg that does all kinds of crazy things before making a single glass of lemonade. If you would like to see a 9 minute video of a Rube Goldberg doing just that, click here.

Here is an example of .reduce in action:

*Screen shot from repl.it
  • In this example, we use .reduce function to get the sum of all the values in the numbers array

.filter

This method takes all the elements of an array and “filters” it by testing each element with in a true or false fashion and then returning a new array with all the ‘truth-y’ elements inside. Think The Bachelor/Bachelorette. The same way a contestant gets to stay around for another week if they pass the Bachelor/Bachelorette’s chemistry test (guess that’s what you call it), an element gets to go into the newly created array if it passes the .filter method’s test.

Here is an example of .filter filtering:

  • Here we use .filter to find the even numbers in the array and return them in a new array.

.map

This method is similar to the .filter method in that it runs a test on the values of the array being passed through and then returns a new array. The .map test, however, runs its test, a function, on each value being passed in the array and returns a new array with the new results created from the function acting on the original array. This is way simpler than it reads. This example should help make it clearer:

  • Here we use map to go through each value in the original array and multiply the number by five. We then get a brand, spank’n new array with our new values.

NOW TO ADD THEM ALL TOGETHER

In the information shown below, we see a variety of artist with some information. You might notice that Creed has only been Platinum Certified 11 times. That puts the amazing alt/rock band well below Shakira’s 18… We need to change that…

  • Using a combination of our three HOF we can filter through the information to find the band we are looking for (Creed), map through the band’s information to find the times the band has been platinum certified and multiply that number by two, which gives us [22]. Now, we can reduce that array to just return a nice clean 22. Which is how many times the band should have gotten platinum certified.

Now this is a pretty weak example of how to use reduce and it is just the tip of the ice berg.

Thanks for your time and thanks for helping me take Creed “higher” than Shakira.

--

--