<?php
require_once("../includes/config.inc.php");
$page_title = "Array Methods: Map, Filter, Reduce";
$page_author = "Joe Zimmerman";
$page_description = "Information about common array methods";
require_once("../includes/header.inc.php");



?>

	    	<!-- Beginning of blog post -->
	    	<div class="blog-post">
	    		<h2 class="blog-post-title">Array Methods: Map, Filter, Reduce</h2>
	    	  	<p class="blog-post-meta">September 17 2020 by Joe Zimmerman</p>

	    	  	<p>A brief overview of helpful array methods.</p>
	    	  	<hr>
	    	  	<h3>Map</h3>
	    	  	<p>
						Array methods like map, filter, and reduce are commonly utilized in 
				functional programming. It is important to know that all of the methods below 
				accept a callback and are defined in Array.prototype, meaning the methods 
				can be used for any array. A callback is a function that is called for every 
				element of the array. Each time the callback executes, the returned value is 
				added to a new array. The map method is helpful when dealing with an array or 
				list in which each element must be updated or returned in the same way. 
				When you call map on an array, it executes the callback on every element within it, 
				returning a new array with all of the values that the callback returned. 
				To demonstrate how the map, filter, and reduce methods work we will use a simple array 
				of integers as an example.
	    	  	</p>
	    	  
	    	  	<!-- <p>
	    	  		Here's a code sample:
	    	  	</p> -->
	    	  	<pre class="language-js"><code>
var intArray = [1,3,5,7];
var newIntArray = intArray.map(function(item){
    return item + 1});
console.log("map: " + newIntArray);
			  	</code></pre>
	    	  	<h3>Filter</h3>
	    	  	<p>
						Filter is another common method that can be used on any array which accepts a callback 
				for the first argument. Unlike the map method, which creates a new array using every 
				element inside an array, using filter will create a new array which will only contain the 
				elements for which the callback returned true.
				</p>
				  <pre class="language-js"><code>
var intArray = [1,3,5,7];
var newIntArray = intArray.filter(function(item){
    return item > 3});
console.log("filter: " + newIntArray);
			  	</code></pre>


				  <h3>Reduce</h3>
	    	  	<p>
						Like the first two methods, the reduce method will also work for any array and accepts 
				a callback as the first argument. Reduce takes in all the elements in an array and reduces 
				them to a single value. Keep in mind that this means the reduce method returns a single value, 
				not an array containing a single item.
				</p>
				  
	    	  
	    	  	<!-- <p>
	    	  		Here's a code sample:
	    	  	</p> -->
	    	  	<pre class="language-js"><code>
var intArray = [1,3,5,7];
var newIntArray = intArray.reduce(function(total, currentValue){
    return total + currentValue});
console.log("reduce: " + newIntArray);
				</code></pre>
	    	</div>
	    	<!-- End of blog post -->

<?php
require_once("../includes/footer.inc.php");
?>