<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mrinal Raj</title>
    <description>The latest articles on DEV Community by Mrinal Raj (@mrinalraj).</description>
    <link>https://dev.to/mrinalraj</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F420620%2F3fe1195e-5e1f-4fee-b59d-5adc1066c84a.jpeg</url>
      <title>DEV Community: Mrinal Raj</title>
      <link>https://dev.to/mrinalraj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mrinalraj"/>
    <language>en</language>
    <item>
      <title>A better way to use fetch api in Javascript</title>
      <dc:creator>Mrinal Raj</dc:creator>
      <pubDate>Mon, 06 Jul 2020 06:08:49 +0000</pubDate>
      <link>https://dev.to/mrinalraj/a-better-way-to-use-fetch-api-in-javascript-4ba0</link>
      <guid>https://dev.to/mrinalraj/a-better-way-to-use-fetch-api-in-javascript-4ba0</guid>
      <description>&lt;p&gt;I have a kind of love-hate relationship with JavaScript. Nonetheless, it has always intrigued me. I have been working on it for the last 3 years now, watching all the design patterns and learning new ones each day.&lt;/p&gt;

&lt;p&gt;What makes a design pattern? How it all starts and how do people start to code something in a specific manner? Facing a new challenge while trying to make something scalable. Yes, that’s the first thing that makes us think about implementation and after that most of the time, we find an already existing way to go about the problem.&lt;br&gt;
This is going to be something similar.&lt;/p&gt;

&lt;p&gt;Let’s understand how we generally write a fetch call in javascript.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Body/arrayBuffer"&gt;&lt;strong&gt;Body.arrayBuffer()&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Body/blob"&gt;&lt;strong&gt;Body.blob()&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Body/formData"&gt;&lt;strong&gt;Body.formData()&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Body/json"&gt;&lt;strong&gt;Body.json()&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Body/text"&gt;&lt;strong&gt;Body.text()&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each of these function again returns a promise, which resolves to the type of data related to each of the functions.&lt;/p&gt;

&lt;p&gt;In a real-world working project, there are numerous fetch calls, and every time if we start writing the above syntax we will simply end up writing so much of boilerplate codes.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;As it is said in programming world. Never repeat yourself.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s try to write a wrapper to make &lt;code&gt;GET&lt;/code&gt; request. This wrapper function will help us keep the headers always same for all the requests and also make sure to keep the base &lt;code&gt;API_URL&lt;/code&gt; consistent throughout the app.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Here the wrapper itself returns a &lt;code&gt;promise&lt;/code&gt;, so we achieved some of the cases that need not be repeated, but I am still not satisfied with writing all the &lt;code&gt;try/catch&lt;/code&gt; blocks.&lt;/p&gt;

&lt;p&gt;This case reminds me of the syntax used in &lt;code&gt;golang&lt;/code&gt;, which goes something like following.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;err, res := myFunction()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here either &lt;code&gt;err&lt;/code&gt; or &lt;code&gt;res&lt;/code&gt; has a null value, depending on if there is an error. We will try and implement a RequestBuilder function that exposes a similar function that returns an array in the structure of &lt;code&gt;[error, response]&lt;/code&gt; .&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Now let’s see what we are doing here. The instance takes in &lt;code&gt;BaseUrl, options, interceptor&lt;/code&gt; as arguments. The &lt;code&gt;BaseUrl&lt;/code&gt; is clearly the base API URL needed by the app. The &lt;code&gt;options&lt;/code&gt; is an object that is passed as the options to the &lt;code&gt;fetch&lt;/code&gt; function. The last one is a function that implements the checks on the status and returns response accordingly.&lt;/p&gt;

&lt;p&gt;Now using this &lt;code&gt;authRequest&lt;/code&gt; object is very easy and also eliminates &lt;code&gt;try/catch&lt;/code&gt; to make our code way cleaner. Here is how.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setLoading(true)

const [error, response] = await authRequest.get(endpoint)

if (error) {
    // handle error
} else {
    // handle success
}

setLoading(false)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Let me know how this article helped you in the comments below.&lt;/p&gt;

&lt;p&gt;Happy coding.&lt;/p&gt;

&lt;p&gt;Thanks to &lt;a href="https://medium.com/@pankajdagar"&gt;Pankaj&lt;/a&gt; for suggesting the topic.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>fetch</category>
      <category>react</category>
    </item>
    <item>
      <title>Javascript module imports in large projects</title>
      <dc:creator>Mrinal Raj</dc:creator>
      <pubDate>Sun, 05 Jul 2020 17:07:39 +0000</pubDate>
      <link>https://dev.to/mrinalraj/javascript-module-imports-in-large-projects-1f41</link>
      <guid>https://dev.to/mrinalraj/javascript-module-imports-in-large-projects-1f41</guid>
      <description>&lt;p&gt;We all have been there, starting off a new project and writing long relative paths as we go ignoring all the trouble it can cause later.&lt;/p&gt;

&lt;p&gt;But let us forget about all the upcoming troubles for a while and assume we will never reach past 15 files in the whole project. Do you actually like writing those dots and slashes? Do the unstructured codes never bother you? Has it never happened that it’s the middle of the night and you those &lt;code&gt;../../../../../../&lt;/code&gt; scare the hell out of you? Anyways, let's see the actual problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;As you keep on scaling your project past a certain point of complexity, you’ll end up traversing up and down your directory structures. Which is first of all &lt;strong&gt;&lt;em&gt;time-consuming and error-prone&lt;/em&gt;&lt;/strong&gt; as well as absence of an IDE can make it a nightmare to remember all directory and files. Even with code-completion features like &lt;a href="https://en.wikipedia.org/wiki/Intelligent_code_completion"&gt;&lt;strong&gt;IntelliSense&lt;/strong&gt;&lt;/a&gt; in place (supported by almost every IDE), this can become more challenging as your codebase grows.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Just imagine a situation where you are changing the directory structure of a module in your project. This will break all the module references and you will be forced to change all occurrences of the module throughout your project. It will be a daunting task!&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/webpack/webpack"&gt;Webpack&lt;/a&gt; allows you to create aliases to &lt;code&gt;import&lt;/code&gt; or &lt;code&gt;require&lt;/code&gt; certain modules through the &lt;code&gt;resolve.alias&lt;/code&gt; property in your config without any additional plugins. Yes, you will have to use &lt;a href="https://github.com/webpack/webpack"&gt;Webpack&lt;/a&gt; for your Nodejs projects.&lt;/p&gt;

&lt;p&gt;How does the &lt;code&gt;webpack.config.js&lt;/code&gt; look? Here are the necessary changes you need to add to the config file.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;After the webpack configuration, the import can be simplified in the following way making is cleaner and less complicated.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Seems fair to configure webpack in a Nodejs project, but what about projects bootstrapped with &lt;a href="https://create-react-app.dev/"&gt;CRA&lt;/a&gt;. Do you React people actually want to run &lt;code&gt;npm eject&lt;/code&gt; just to manage aliases? That can be even greater pain than those longer relative imports.&lt;/p&gt;

&lt;h2&gt;
  
  
  CRACO saves the day
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/package/@craco/craco"&gt;CRACO&lt;/a&gt; short for &lt;strong&gt;C&lt;/strong&gt;reate &lt;strong&gt;R&lt;/strong&gt;eact &lt;strong&gt;A&lt;/strong&gt;pp &lt;strong&gt;C&lt;/strong&gt;onfig &lt;strong&gt;O&lt;/strong&gt;verride is an npm package that replaces the default &lt;code&gt;react-scripts&lt;/code&gt; in a CRA project and overrides some of the webpack configurations. CRACO with &lt;code&gt;craco-alias&lt;/code&gt; plugin can help us achieve the same. &lt;br&gt;
Here is how the configuration file will look like.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Are we done?
&lt;/h2&gt;

&lt;p&gt;Apparently, all of these configurations absolutely breaks the &lt;a href="https://en.wikipedia.org/wiki/Intelligent_code_completion"&gt;&lt;strong&gt;IntelliSense&lt;/strong&gt;&lt;/a&gt; for these imports in &lt;a href="https://code.visualstudio.com/"&gt;Visual Studio Code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Luckily, IDEs like &lt;a href="https://code.visualstudio.com/"&gt;Visual Studio Code&lt;/a&gt; have the option to let them know how to resolve these aliases just by adding a &lt;code&gt;jsconfig.json&lt;/code&gt; file with appropriate options. Just like the one below.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Yeah yeah, that's all, I know it's a lot of boilerplate but in an enterprise-level project where codebase grows every day, it's a must-have. And that sums up everything you need to know for creating aliases for Javascript imports.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Enjoyed reading the piece? A few Claps and Shares are all I need. Cheers.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webpack</category>
      <category>webdev</category>
      <category>react</category>
    </item>
    <item>
      <title>Javascript in real world</title>
      <dc:creator>Mrinal Raj</dc:creator>
      <pubDate>Sun, 05 Jul 2020 16:15:56 +0000</pubDate>
      <link>https://dev.to/mrinalraj/javascript-in-real-world-4a0p</link>
      <guid>https://dev.to/mrinalraj/javascript-in-real-world-4a0p</guid>
      <description>&lt;p&gt;If you’re starting in JavaScript, you may be busy doing DOM manipulation and playing with other browser APIs. Although, these are also a part of javascript, when it comes to industrial use of the language for frontend frameworks like React or Angular, or with backend frameworks like express.js or sails.js yo come across tons of arrays and object manipulations, and maybe you haven’t heard of .map(), .reduce(), and .filter(). If you don’t need to be compatible with this very old browser, you have to become familiar with those methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  .map()
&lt;/h2&gt;

&lt;p&gt;Let me explain how it works with a simple example. Say you have received an array containing multiple objects — each one representing a person. But you only need the list of names from that.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// What you have
var officers = [
  { id: 20, name: 'Captain America' },
  { id: 24, name: 'Spiderman' },
  { id: 56, name: 'Iron Man' },
  { id: 88, name: 'Hulk' }
];
// What you need
['Captain America', 'Spiderman', 'Iron Man', 'Hulk']
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There are multiple ways to achieve this. You might want to do it by creating an empty array, then using any of .forEach(), .for(...of), or a simple .for() to meet your goal. We will do it by using &lt;strong&gt;Array.prototype.map()&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var officersNames = officers.map(function (officer) {
  return officer.name
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I used the function keyword to create an anonymous function here, we can even be more concise with arrow functions (requires ES6 support, Babel or TypeScript)&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const officersName = officers.map(officer =&amp;gt; officer.name);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So how does .map() work? Basically it takes 2 arguments, a callback and an optional context (will be considered as this in the callback) which I did not use in the previous example. The callback runs for &lt;strong&gt;each value in the array&lt;/strong&gt; and &lt;strong&gt;returns each new value&lt;/strong&gt; in the resulting array. There can be numerous possible applications of map in a real-world application. For example in React.js to render a list of cards with these names we can simply create an array of cards using the same map, as shown below.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const officerCards = officers.map(officer =&amp;gt; &amp;lt;Card name={officer}/&amp;gt;);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This array can now be placed directly to render the list of cards, with each card having the names.&lt;/p&gt;

&lt;p&gt;Keep in mind that the resulting array will always be the same length as the original array.&lt;/p&gt;

&lt;h2&gt;
  
  
  .reduce()
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;The accumulator can be anything (integer, string, object, etc.) and must be instantiated or passed when calling the function.&lt;/p&gt;

&lt;p&gt;Time for an example! You have an array with these students and their respective age:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var students = [
  {
    id: 10,
    name: "John Doe",
    age: 14,
  },
  {
    id: 2,
    name: "Jane Roe",
    age: 30,
  },
  {
    id: 41,
    name: "Foo",
    age: 16,
  },
  {
    id: 99,
    name: "Bar",
    age: 22,
  }
];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We need to know the total age of all of them. With .reduce(), it’s pretty straightforward:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var totalAge = students.reduce(function (accumulator, student) {
  return accumulator + student.age;
}, 0);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Notice that I’ve set the starting value as 0. I could have also used an existing variable if necessary. After running the callback for each element of the array, reduce will return the final value of our accumulator (in our case: 82).&lt;/p&gt;

&lt;p&gt;Now again let shorten it with the ES6 arrow function.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const totalAge = students.reduce((acc, student) =&amp;gt; acc + student.age, 0);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now let’s say I want to find which student is the oldest. For that, I can use reduce as well:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var oldestStudent = students.reduce(function (oldest, student) {
  return (oldest.age || 0) &amp;gt; student.age ? oldest : student;
}, {});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I named my accumulator oldest. My callback compares the accumulator to each student. If a student has more age than oldest, then that student becomes the new oldest so that’s the one I return.&lt;/p&gt;

&lt;p&gt;As you can see, using .reduce() is an easy way to generate a single value or object from an array.&lt;/p&gt;

&lt;h2&gt;
  
  
  .filter()
&lt;/h2&gt;

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

&lt;p&gt;Here’s our data:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var pilots = [
  {
    id: 2,
    name: "Wedge Antilles",
    faction: "Rebels",
  },
  {
    id: 8,
    name: "Ciena Ree",
    faction: "Empire",
  },
  {
    id: 40,
    name: "Iden Versio",
    faction: "Empire",
  },
  {
    id: 66,
    name: "Thane Kyrell",
    faction: "Rebels",
  }
];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Say we want two arrays now: one for rebel pilots, the other one for imperials. With .filter() it couldn’t be easier!&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var rebels = pilots.filter(function (pilot) {
  return pilot.faction === "Rebels";
});
var empire = pilots.filter(function (pilot) {
  return pilot.faction === "Empire";
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That’s it! And it’s even shorter with arrow functions:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const rebels = pilots.filter(pilot =&amp;gt; pilot.faction === "Rebels");
const empire = pilots.filter(pilot =&amp;gt; pilot.faction === "Empire");
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Basically, if the callback function &lt;strong&gt;returns true&lt;/strong&gt;, the current element &lt;strong&gt;will be in the resulting array&lt;/strong&gt;. If it returns false, it won’t be.&lt;/p&gt;

&lt;h2&gt;
  
  
  .find()
&lt;/h2&gt;

&lt;p&gt;Where filter always returns an array, which can have zero, one or more than one element, find can be used to get a single element out of the array. Similar to filter() find takes a function as an argument, iterates over the elements of the array and return the first element for which the function gives return true&lt;/p&gt;

&lt;p&gt;Let’s try on the same data as the previous one:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var pilots = [
  {
    id: 2,
    name: "Wedge Antilles",
    faction: "Rebels",
  },
  {
    id: 8,
    name: "Ciena Ree",
    faction: "Empire",
  },
  {
    id: 40,
    name: "Iden Versio",
    faction: "Empire",
  },
  {
    id: 66,
    name: "Thane Kyrell",
    faction: "Rebels",
  }
];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Say we want to find the first imperial pilot, find() can do this with the following one-liner.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pilots.find(pilot =&amp;gt; pilot.faction === "Empire")

// {
    id: 8,
    name: "Ciena Ree",
    faction: "Empire",
  }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;
  
  
  Combining .map(), .reduce(), and .filter()
&lt;/h2&gt;

&lt;p&gt;Since all three are called on arrays and since .map() and .filter() both return arrays, we can easily chain our calls.&lt;/p&gt;

&lt;p&gt;Let’s check out another example. Here’s our data:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var personnel = [
  {
    id: 5,
    name: "Luke Skywalker",
    pilotingScore: 98,
    shootingScore: 56,
    isForceUser: true,
  },
  {
    id: 82,
    name: "Sabine Wren",
    pilotingScore: 73,
    shootingScore: 99,
    isForceUser: false,
  },
  {
    id: 22,
    name: "Zeb Orellios",
    pilotingScore: 20,
    shootingScore: 59,
    isForceUser: false,
  },
  {
    id: 15,
    name: "Ezra Bridger",
    pilotingScore: 43,
    shootingScore: 67,
    isForceUser: true,
  },
  {
    id: 11,
    name: "Caleb Dume",
    pilotingScore: 71,
    shootingScore: 85,
    isForceUser: true,
  },
];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Our objective: get the total score of force users only. Let’s do it step by step!&lt;/p&gt;

&lt;p&gt;First, we need to filter out the person who can’t use the force:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var jediPersonnel = personnel.filter(function (person) {
  return person.isForceUser;
});// Result: [{...}, {...}, {...}] (Luke, Ezra and Caleb)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;With that, we have 3 elements left in our resulting array. We now need to create an array containing the total score of each Jedi.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var jediScores = jediPersonnel.map(function (jedi) {
  return jedi.pilotingScore + jedi.shootingScore;
});// Result: [154, 110, 156]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And let’s use reduce to get the total:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var totalJediScore = jediScores.reduce(function (acc, score) {
  return acc + score;
}, 0);// Result: 420
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

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

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And look how pretty it is with arrow functions:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const totalJediScore = personnel
  .filter(person =&amp;gt; person.isForceUser)
  .map(jedi =&amp;gt; jedi.pilotingScore + jedi.shootingScore)
  .reduce((acc, score) =&amp;gt; acc + score, 0);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Boom! 💥&lt;/p&gt;

&lt;p&gt;Keep coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>array</category>
      <category>arraymethods</category>
      <category>javascripttips</category>
    </item>
  </channel>
</rss>
