{"id":5653,"date":"2022-05-22T17:16:00","date_gmt":"2022-05-22T22:16:00","guid":{"rendered":"https:\/\/upmostly.com\/?p=5653"},"modified":"2022-03-21T17:16:47","modified_gmt":"2022-03-21T22:16:47","slug":"how-to-reduce-an-array-in-react","status":"publish","type":"post","link":"http:\/\/upmostly.com\/tutorials\/how-to-reduce-an-array-in-react","title":{"rendered":"How To Reduce An Array in React?"},"content":{"rendered":"\n<p>So you have an array in the state of your application and need to process it somehow? Let&#8217;s take a look at how to write a simple algorithm to sum all the values of the array. Later we will improve on it by using the <strong>reduce<\/strong> JS method available on the arrays.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Algorithm<\/h2>\n\n\n\n<p>Given an array of objects, where each object has two properties, <strong>name<\/strong> and <strong>age<\/strong>, calculate the average age of all the users. We will first do it in the simplest way possible, by utilising the for loops that iterate over the array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  const users = &#091;{ name: 'Tom', age: 21}, { name: 'Mike', age: 23}, { name: 'Anna', age: 54}]\n\n  const getAverageAge = (users) =&gt; {\n    let sum = 0\n    for (let i = 0; i &lt; users.length; i++) {\n      sum += users&#091;i].age\n    }\n    return sum \/ users.length\n  }\n\ngetAverageAge(users) \/\/ Result is 32.66....<\/code><\/pre>\n\n\n\n<p>In the body of the <strong>getAverageAge<\/strong> function, we&#8217;re creating a new local variable <strong>sum<\/strong>. We&#8217;re then iterating over the array of users by using the for loop, inside the loop code block we&#8217;re grabbing each of the user&#8217;s<strong> age <\/strong>and adding it to the total <strong>sum<\/strong>. As the last step, we&#8217;re dividing the <strong>sum<\/strong> variable by the total length of the input array.<\/p>\n\n\n\n<p>On the code level, there is absolutely nothing wrong with the above implementation. It does the job, but it&#8217;s not very readable and as it turns out there is clearly a built it method on the array that can help us handle this operation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reduce<\/h2>\n\n\n\n<p>Let&#8217;s take advantage of the <strong>reduce<\/strong> function to simplify the above code snippet just a little bit. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  const users = &#091;{ name: 'Tom', age: 21}, { name: 'Mike', age: 23}, { name: 'Anna', age: 54}]\n\n  const getAverageAge = (users) =&gt; {\n    const sum = users.reduce((prev, curr, index, array) =&gt; prev + curr.age, 0)\n    return sum \/ users.length\n  }\n\ngetAverageAge(users) \/\/ Result is 32.66....<\/code><\/pre>\n\n\n\n<p>Do you see what happened there? Let&#8217;s break it down.<\/p>\n\n\n\n<p>Instead of making use of the for loop, we&#8217;re using the <strong>reduce<\/strong> method. It takes two arguments with the first one being a <strong>callback function<\/strong> and the second one the <strong>initial value<\/strong>. Very often the callback function tends to be simple and can be written as an arrow function to simplify things even further.<\/p>\n\n\n\n<p>The <strong>callback function<\/strong> takes four arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>previous value of the resulting call to the function (on first iteration it&#8217;s set to the <strong>initial value<\/strong>, which is 0 in our case)<\/li><li>current value of the current element (given that <strong>users<\/strong> is an array of objects, we&#8217;re reaching for the age property of each object)<\/li><li>index postion of the current element in the array (we are not using it)<\/li><li>the array on which we called the reduce mathod itself (we are not using it either)<\/li><\/ul>\n\n\n\n<p>For a more detailed syntax explanation please check the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/Reduce\" target=\"_blank\" rel=\"noopener\">MDN web docs<\/a>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>o you have an array in the state of your application and need to process it somehow? Let&#8217;s take a look at how to write a simple algorithm to sum all the values of the array.<\/p>\n","protected":false},"author":116,"featured_media":5662,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[4],"class_list":["post-5653","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-react"],"acf":[],"_links":{"self":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts\/5653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/users\/116"}],"replies":[{"embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/comments?post=5653"}],"version-history":[{"count":5,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts\/5653\/revisions"}],"predecessor-version":[{"id":5671,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts\/5653\/revisions\/5671"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/media\/5662"}],"wp:attachment":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/media?parent=5653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/categories?post=5653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/tags?post=5653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}