{"id":3343,"date":"2021-02-26T16:42:51","date_gmt":"2021-02-26T16:42:51","guid":{"rendered":"https:\/\/thenextweb.com\/?p=1340616"},"modified":"2021-02-26T16:42:51","modified_gmt":"2021-02-26T16:42:51","slug":"the-ins-and-outs-of-javascript-reducer-a-simple-yet-powerful-array-method","status":"publish","type":"post","link":"https:\/\/www.londonchiropracter.com\/?p=3343","title":{"rendered":"The ins and outs of JavaScript reducer \u2014 a simple, yet powerful array method"},"content":{"rendered":"\n<p>JavaScript\u2019s <code>reduce<\/code> is one of the most useful array methods that should be in a developer\u2019s arsenal. Introduced in ES5, it\u2019s somewhat similar to <code>for\u2026each<\/code> and <code>map<\/code> methods that are used with arrays, but improves on their performance and simplicity in specific situations.<\/p>\n<p>The <code>reduce<\/code> method executes a callback function that we provide on each element stored in an array and outputs the final value the operation generates. It\u2019s a cleaner way of iterating over and processing the data stored in an array.<\/p>\n<p>Currently, it\u2019s<span>&nbsp;<\/span><a href=\"https:\/\/caniuse.com\/mdn-javascript_builtins_array_reduce\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">supported by all of the major browser versions<\/a>,&nbsp;<span>and<\/span> is available in Node.js from version 10.0 upwards.<\/p>\n<p>Today, we are going to explore this&nbsp;<code>reduce<\/code><span>&nbsp;method<\/span>; more specifically, we\u2019ll go through how and when to use it in different scenarios.<\/p>\n<p>Let\u2019s get started then!<\/p>\n<h2>Javascript reduce method parameters<\/h2>\n<p>The <code>reduce<\/code> method accepts two arguments: a reducer function for the array that is used as a callback,&nbsp;and an optional<span>&nbsp;<\/span><code>initialValue<\/code><span>&nbsp;<\/span>argument. The reducer function takes four arguments:<span>&nbsp;<\/span><code>accumulator<\/code>,<span>&nbsp;<\/span><code>currentValue<\/code>,<span>&nbsp;<\/span><code>currentIndex<\/code>, and&nbsp;<code>array<\/code>.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1340617 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.48.41.png\" alt width=\"415\" height=\"61\" sizes=\"(max-width: 415px) 100vw, 415px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.48.41.png 680w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.48.41-280x41.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.48.41-540x79.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.48.41-270x40.png 270w\"><\/figure>\n<p><\/span><\/p>\n<p>An example of Javascript array<span>&nbsp;<\/span><strong>reduce<\/strong><span>&nbsp;method<\/span> in action:<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1340618 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.49.45.png\" alt width=\"620\" height=\"66\" sizes=\"(max-width: 620px) 100vw, 620px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.49.45.png 1018w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.49.45-280x30.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.49.45-540x57.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.49.45-270x29.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.49.45-796x84.png 796w\"><\/figure>\n<p><\/span><\/p>\n<p>The This&nbsp;<strong>reduce<\/strong><span>&nbsp;method<\/span> does the same job as the following <code>for\u2026each<\/code> loop, but with fewer lines of code.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1340619 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.50.44.png\" alt width=\"339\" height=\"209\" sizes=\"(max-width: 339px) 100vw, 339px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.50.44.png 418w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.50.44-280x173.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.50.44-219x135.png 219w\"><\/figure>\n<p><\/span><\/p>\n<p>How does the<span>&nbsp;<\/span><code>reduce<\/code><span>&nbsp;<\/span>method achieve it using these parameters?<\/p>\n<p>The value returned by the reducer function is assigned to the<span>&nbsp;<\/span><code>accumulator<\/code><span>&nbsp;<\/span>variable. In each iteration through the array items, the accumulator\u2019s value is updated to the returned result. At the end of the iteration, the final value of the accumulator is returned as the output of the<span>&nbsp;<\/span><code>reduce<\/code><span>&nbsp;function<\/span>.<\/p>\n<p>If an<span>&nbsp;<\/span><code>initialValue<\/code><span>&nbsp;<\/span>argument is passed, the first time the reducer function is executed, the<span>&nbsp;<\/span><code>accumulator<\/code>&nbsp;will be equal to<span>&nbsp;<\/span><code>initialValue<\/code><span>&nbsp;and<\/span> the<span>&nbsp;<\/span><code>currentValue<\/code><span>&nbsp;will<\/span> be equal to the first element stored in the array. If an initialValue is not passed, the<span>&nbsp;<\/span><code>accumulator<\/code>&nbsp;will be equal to the first value of the array and&nbsp;<code>currentValue<\/code><span>&nbsp;will<\/span> be equal to the second.<\/p>\n<p>Let\u2019s see how the values of each of these parameters change every time the callback function is called in the following example. Here, we are not passing an<span>&nbsp;<\/span><code>initialValue<\/code><span>&nbsp;<\/span>argument.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1340620 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.51.20.png\" alt width=\"799\" height=\"94\" sizes=\"(max-width: 799px) 100vw, 799px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.51.20.png 1022w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.51.20-280x33.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.51.20-540x63.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.51.20-270x32.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.51.20-796x93.png 796w\"><\/figure>\n<p><\/span><\/p>\n<p><figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1340621 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.51.37.png\" alt width=\"709\" height=\"406\" sizes=\"(max-width: 709px) 100vw, 709px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.51.37.png 1052w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.51.37-280x160.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.51.37-472x270.png 472w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.51.37-236x135.png 236w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.51.37-796x456.png 796w\"><\/figure>\n<\/p>\n<p>The final output of this function is<span>&nbsp;<\/span><code>10<\/code>.<\/p>\n<p>Next, let\u2019s see how it works when an<span>&nbsp;<\/span><code>initialValue<\/code><span>&nbsp;<\/span>is passed.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1340622 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.52.31.png\" alt width=\"757\" height=\"89\" sizes=\"(max-width: 757px) 100vw, 757px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.52.31.png 1022w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.52.31-280x33.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.52.31-540x63.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.52.31-270x32.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.52.31-796x93.png 796w\"><\/figure>\n<p><\/span><\/p>\n<p><figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1340623 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.53.16.png\" alt width=\"726\" height=\"509\" sizes=\"(max-width: 726px) 100vw, 726px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.53.16.png 1050w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.53.16-280x196.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.53.16-385x270.png 385w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.53.16-193x135.png 193w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.53.16-796x558.png 796w\"><\/figure>\n<\/p>\n<p>This function outputs the value&nbsp;<code>22<\/code>.<\/p>\n<h2 id=\"when-to-use-javascript-reducer\">When to use JavaScript\u2019s reducer<\/h2>\n<p>The <code>reduce<\/code>&nbsp;method provides a unique way to iterate through items in an array and process them. So what are the situations in which we can benefit from this uniqueness?<\/p>\n<h2 id=\"calculate-the-sum-of-values-in-an-array\">Calculate the sum of values in an array<\/h2>\n<p>This is similar to what we did in previous examples. The only difference is we have to pass 0 for the<span>&nbsp;<\/span><code>initialValue<\/code><span>&nbsp;<\/span>parameter.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1340626 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.56.01.png\" alt width=\"804\" height=\"99\" sizes=\"(max-width: 804px) 100vw, 804px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.56.01.png 988w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.56.01-280x35.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.56.01-540x67.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.56.01-270x33.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.56.01-796x98.png 796w\"><\/figure>\n<p><\/span><\/p>\n<h2 id=\"flatten-an-array\">Flatten an array<\/h2>\n<p>If we have an array of arrays, we can use the reduce method to flatten it and create a single array without nested arrays.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1340627 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.56.36.png\" alt width=\"809\" height=\"184\" sizes=\"(max-width: 809px) 100vw, 809px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.56.36.png 922w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.56.36-280x64.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.56.36-540x123.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.56.36-270x61.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.56.36-796x181.png 796w\"><\/figure>\n<p><\/span><\/p>\n<p>We pass an empty array as the initial value so the items in the first array are concatenated with it to create a flattened array.<\/p>\n<p>If the first array has more than one level of nested arrays, we can recursively call the reduce function to flatten and then concatenate them with the final array.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1340628 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.57.14.png\" alt width=\"1078\" height=\"532\" sizes=\"(max-width: 1078px) 100vw, 1078px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.57.14.png 1078w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.57.14-280x138.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.57.14-540x266.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.57.14-270x133.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.57.14-796x393.png 796w\"><\/figure>\n<p><\/span><\/p>\n<p>If the current value accepted by the callback is an array, as verified using the <code>isArray<\/code> method, we recursively call the <code>flattenArray<\/code> function on it. If the current value is not an array, we simply concatenate the value with the final flattened array.<\/p>\n<h2 id=\"grouping-an-array-of-objects-by-a-property\">Grouping an array of objects by a property<\/h2>\n<p>Assume that we have an array of&nbsp;objects that are basically the names of countries \u2014 and&nbsp;we want to group each country in the array according to their continents. We can use the <code>reduce<\/code> method for this task. Check out the code snippet below:<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1340629 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.57.52.png\" alt width=\"631\" height=\"507\" sizes=\"(max-width: 631px) 100vw, 631px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.57.52.png 774w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.57.52-261x210.png 261w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.57.52-336x270.png 336w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.57.52-168x135.png 168w\"><\/figure>\n<p><\/span><\/p>\n<p>Inside the callback function, we create a new key for each continent that is not in the groupedCountries map and assign an empty array as its value. Then we push each country object to the array stored by their respective continents.<\/p>\n<h2 id=\"using-reduce-in-place-of-filtermap\">Using reduce() in place of filter().map()<\/h2>\n<p>In JavaScript, we use the <code>filter<\/code> method to filter items stored in an array using a callback. We use the map method to create a new array using the old array using the logic passed inside a callback. Sometimes we have to use these two methods, one after the other to create a new array with the results we filter using some conditions.<\/p>\n<p>Instead of using two array methods, you can use the JavaScript array&nbsp;<code>reduce<\/code>&nbsp;method to complete the same task. It will reduce the completion time because now you only iterate through the array only once, not twice.<\/p>\n<p>For example, let\u2019s take the following scenario where we want to create an array of square roots of numbers greater than 30.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1340630 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.58.26.png\" alt width=\"804\" height=\"166\" sizes=\"(max-width: 804px) 100vw, 804px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.58.26.png 1038w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.58.26-280x58.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.58.26-540x111.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.58.26-270x56.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.58.26-796x164.png 796w\"><\/figure>\n<p><\/span><\/p>\n<p>The same scenario implemented using reduce looks like this.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1340631 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.59.07.png\" alt width=\"799\" height=\"305\" sizes=\"(max-width: 799px) 100vw, 799px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.59.07.png 886w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.59.07-280x107.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.59.07-540x206.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.59.07-270x103.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-09.59.07-796x304.png 796w\"><\/figure>\n<p><\/span><\/p>\n<p>Inside the callback, we simply check if the number is greater than 30 and add its square root to the&nbsp;<code>accumulator<\/code>&nbsp;array. You have to pass an empty array as the initial value to get this result.<\/p>\n<h2 id=\"build-your-own-reducer\">Build your own reducer<\/h2>\n<p>In this section, we are going to implement the reducer function on our own to see how things work under the hood. This will give you a better idea of when to use the JavaScript reducer for optimal performance in your program.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1340632 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-10.00.49.png\" alt width=\"806\" height=\"527\" sizes=\"(max-width: 806px) 100vw, 806px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-10.00.49.png 1006w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-10.00.49-280x183.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-10.00.49-413x270.png 413w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-10.00.49-206x135.png 206w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/02\/Screenshot-2021-02-26-at-10.00.49-796x521.png 796w\"><\/figure>\n<p><\/span><\/p>\n<p>First, we check if the reduce method was called on a null or undefined object. Then we check if the passed callback is a function.<\/p>\n<p>After the initial type checks, we assign the passed <code>initialValue<\/code>to the accumulator. Then we loop through the array and call the callback for each item in the array. At the end of execution, we have to return the accumulator value.<\/p>\n<p>We are using this implementation only to help you understand how the reduce method actually works. For example, you can see that it uses a <code>for<\/code>&nbsp;loop to iterate through the array under the hood.<\/p>\n<p>However, note that you shouldn\u2019t use this implementation in your production code. In fact, prototyping methods to JavaScript standard types is a bad coding practice you should never indulge in.<\/p>\n<p>I hope this knowledge will help you to identify problems that can be resolved with the reducer in the future. Some of these use cases overlap with<span>&nbsp;the&nbsp;<\/span><a href=\"https:\/\/livecodestream.dev\/post\/javascript-foreach-powered-array-for-loop\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">forEach<\/a><span>&nbsp;<\/span>,<span>&nbsp;<\/span><code>map<\/code>, and<span>&nbsp;<\/span><code>filter<\/code>&nbsp;array methods. So you should know to pick the situations that can be solved optimally using the <code>reduce<\/code>&nbsp;method.<\/p>\n<p><i><span>This <\/span><\/i><a href=\"https:\/\/livecodestream.dev\/post\/javascript-reducer-a-simple-yet-powerful-array-method\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>article<\/span><\/i><\/a><i><span> was originally published on <\/span><\/i><a href=\"https:\/\/livecodestream.dev\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>Live Code Stream<\/span><\/i><\/a><i><span> by <\/span><\/i><a href=\"https:\/\/www.linkedin.com\/in\/bajcmartinez\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>Juan Cruz Martinez<\/span><\/i><\/a><i><span> (twitter: <\/span><\/i><a href=\"https:\/\/twitter.com\/bajcmartinez\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>@bajcmartinez<\/span><\/i><\/a><i><span>), founder and publisher of Live Code Stream, entrepreneur, developer, author, speaker, and doer of things.<\/span><\/i><\/p>\n<p><a href=\"https:\/\/livecodestream.dev\/subscribe\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>Live Code Stream<\/span><\/i><\/a><i><span> is also available as a free weekly newsletter. Sign up for updates on everything related to programming, AI, and computer science in general.<\/span><\/i><\/p>\n<p> <a href=\"https:\/\/thenextweb.com\/dd\/2021\/02\/26\/javascript-reducer-simple-yet-powerful-array-method-syndication\/\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript\u2019s reduce is one of the most useful array methods that should be in a developer\u2019s arsenal. Introduced in ES5, it\u2019s somewhat similar to for\u2026each and map methods that are used with&#8230;<\/p>\n","protected":false},"author":1,"featured_media":3344,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts\/3343"}],"collection":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3343"}],"version-history":[{"count":0,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts\/3343\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/media\/3344"}],"wp:attachment":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}