Shaping Data with JavaScript: Part 3 of the Code Fellows “Mastering Arrays” series

Image for post
Image for post

As humans, we adapt continuously … ingesting data, processing it, acting on it, and producing a result. It’s never a 1:1… Hot + Sunny doesn’t necessarily mean T-Shirt + Shades. Sometimes it just means Air Conditioning. When we look at complex data, sometimes we want to perform analysis and operations on it, to roll it up into something new. Often, we need the list of data points as input to produce something totally different on the other side … Adapting it to solve a problem, while reducing the noise.

We do the same thing in code. When we need to pull the substance out of a noisy data set in JavaScript, we turn to the reduce() method.

What is the JS Array Reduce Method?

As per MDN:

Array.prototype.reduce() : executes a reducer function (that you provide) on each element of the array, resulting in single output value.

That means we get a new, likely smaller, array with just the values meeting the conditions we specify. Great!

How do I use it?

Let’s see it in action!

Originally published at https://www.codefellows.org.

Learn to Code. Get a Job. Start Here! Learn more at www.codefellows.org

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store