And snorkeling your way through every last one… err… not very romantic.
So let’s master the most commonly used functions. And save the others for another time.
The filter function is used to get rid of any elements in an array that you don’t want.
A simple example — remove all odd numbers from an array.
You will notice that inside of the filter function we specify a test to make sure that the element is even. Every element that is even gets added to the new numbers array.
The reduce functions calls a reducer (that you provide) on every element in the array and reduces the output to a single value.
Say we want to find the total sum of an array of numbers. The reduce function is the best way to do it.
The reduce function accepts multiple arguments. In this example, we used the first two which are:
- The accumulated value.
- The current element being processed in the array.
The map function creates a new array by calling a function on every element in the old array.
For example, we want to double the value of every element in an array.
You can use map to call a provided function on every element in an array.
The simple secret you can use to learn how to write high quality code — FASTER!
How do you learn how to write better code? Without blowing years learning the art of writing software? Here’s a simple…
Liked this article? Please give it a hearty 👏— thank-you!