Simplify your JavaScript – Use .map(), .reduce(), and .filter()

What if you have an array, but only want some of the elements in it? That’s where .filter() comes in!

Combining .map(), .reduce(), and .filter()

Since all three are called on arrays and since .map() and .filter() both return arrays, we can easily chain our calls.

 

And now here’s the fun part… we can chain all of this to get what we want in a single line:

var totalJediScore = personnel
  .filter(function (person) {
    return person.isForceUser;
  })
  .map(function (jedi) {
    return jedi.pilotingScore + jedi.shootingScore;
  })
  .reduce(function (acc, score) {
    return acc + score;
  }, 0);

And look how pretty it is with arrow functions:

const totalJediScore = personnel
  .filter(person => person.isForceUser)
  .map(jedi => jedi.pilotingScore + jedi.shootingScore)
  .reduce((acc, score) => acc + score, 0);

Simplify your JavaScript – Use .map(), .reduce(), and .filter()

.map()

Let me explain how it works with a simple example. Say you have received an array containing multiple objects – each one representing a person. The thing you really need in the end, though, is an array containing only the id of each person.

.reduce()

Just like .map().reduce() also runs a callback for each element of an array. What’s different here is that reduce passes the result of this callback (the accumulator) from one array element to the other.

.filter()

What if you have an array, but only want some of the elements in it? That’s where .filter() comes in!

 

And now here’s the fun part… we can chain all of this to get what we want in a single line:

var totalJediScore = personnel
  .filter(function (person) {
    return person.isForceUser;
  })
  .map(function (jedi) {
    return jedi.pilotingScore + jedi.shootingScore;
  })
  .reduce(function (acc, score) {
    return acc + score;
  }, 0);

And look how pretty it is with arrow functions:

const totalJediScore = personnel
  .filter(person => person.isForceUser)
  .map(jedi => jedi.pilotingScore + jedi.shootingScore)
  .reduce((acc, score) => acc + score, 0);

JavaScript Functional Programming — map, filter and reduce

map is used when you have an array of stuff ( scientific term ) and you want todo something ( another scientific term ) for every item in that array.

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

The reduce() method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.

Sanjay Ghemawat

Sanjay Ghemawat (born 1966) is an Indian American[1] computer scientist and software engineer. He is currently a Senior Fellow at Google in the Systems Infrastructure Group.[2][3] Ghemawat’s work at Google, much of it in close collaboration with Jeff Dean,[4] has included big data processing model MapReduce, the Google File System, and databases Bigtable and SpannerWired have described him as one of the “most important software engineers of the internet age”.[4]

 

Ghemawat studied at Cornell University and the Massachusetts Institute of Technology (MIT).[2] He obtained a PhD from MIT in 1995, with a dissertation titled, The Modified Object Buffer: A Storage Management Technique for Object-Oriented Databases. His advisorswere Barbara Liskov and Frans Kaashoek.[5]

Before joining Google, Ghemawat worked at the DEC Systems Research Center. There he began his longtime collaboration with Jeff Dean, who worked at another DEC research lab nearby. Their work at DEC included a Java compiler and a system profiling tool.[4]