Skip to content

Commit fa17764

Browse files
author
Peter F. Tumulty
committed
rebuild the website
1 parent 9faf50e commit fa17764

File tree

4 files changed

+144
-53
lines changed

4 files changed

+144
-53
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1-
# Using JavaScripts's Map & forEach Methods
1+
# Comparing Array Methods in JavaScript
2+
3+
This is a simple demonstration on how to use ```map()``` and ```forEach()``` methods in javascript. and explaination of how they work, and a comparission between these two array methods.
4+
5+
## Motivation
6+
7+
## Build status
8+
9+
## Installation
10+
11+
12+
13+
14+
15+
216

317
This code compares how **.map** and **.forEach** handle the same set of data. The intention of comparison is to discover how .map works on a deeper level. I wrote a full article detailing what I discovered on Medium.com. [Click here to read the full article](https://medium.com/@petertumulty/the-power-of-the-map-method-4db6b1a73655)

app.js

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,15 @@
1-
$(document).ready(function() {
1+
const myWebStack = ['JavaScript', 'HTML', 'CSS', 'somtimes PHP', 'sometimes Python', 'sometimes SaSS'];
22

3-
function TransformArray(element) {
4-
return element.first.toLowerCase();
5-
return element.last.toLowerCase();
6-
}
3+
// the DOM element I'm going to set my results too. I want to display the results of the forEach method
4+
// in HTML.
5+
const firstMapElm = document.getElementById('first-foreach');
76

8-
$.getJSON("https://immense-gorge-15710.herokuapp.com/experts", function(data) {
9-
10-
var data = data;
11-
console.log("So what's the difference between forEach & .map ?");
12-
console.log("--------------")
13-
14-
data.forEach(function(value, index, array) {
15-
console.log("forEach: using value: " + value.first);
16-
console.log("forEach: using array[index]: " + array[index].first);
17-
})
18-
19-
console.log('------------');
20-
console.log('Using forEach, array[index] returns the same out put as value.');
21-
console.log('------------');
22-
23-
data.map(function(value, index, array) {
24-
console.log(".map: using value: " + value.first)
25-
console.log(".map: array[index]: " + array[index].first);
26-
})
7+
// executing the forEach method and setting the results to a variable called executingForEachOverItems
8+
const t = myWebStack.forEach(function(item, index, array) {
9+
return item;
10+
});
2711

28-
console.log('------------');
29-
console.log('Using .map, array[index] returns the same out put as value.');
30-
console.log('------------');
31-
console.log("Why bother having a .map and a forEach? Why not just have one or the other? ");
32-
console.log("As you can see that both the forEach and .map both take in three arguments and do exactly the same thing. So there really isn't a need for both, right? Ah not necessarily...")
33-
console.log('------------');
34-
console.log(data.forEach(TransformArray));
35-
console.log("The undefined you are seeing above occurs because we are trying to run the TransformArray function on the data in the forEach.");
36-
console.log("Why doesn't this work? It doesn't work because the forEach method is simple and only spits back what you put in. The callback function inside the forEach method is designed to only spit back the data you put in it.")
37-
console.log(data.map(TransformArray));
38-
console.log("The data you are seeing is when you run the TransfromArray function inside the .map parameters. It works because the callback function inside the .map parameters is designed for the data being passed in it to be mutuble.");
12+
console.log(t);
3913

40-
});
41-
});
14+
// adding the results of the map method over my array to HTML
15+
firstMapElm.innerHTML= JSON.stringify(t)

data.json

Whitespace-only changes.

index.html

Lines changed: 118 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,125 @@
1-
<!doctype html>
2-
<html class="no-js" lang="">
1+
2+
<html lang="en">
33
<head>
44
<meta charset="utf-8">
5-
<meta name="description" content="">
6-
<meta name="viewport" content="width=device-width, initial-scale=1">
7-
<title>.map vs. forEach</title>
5+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
6+
<title>Array Methods in JavaScript</title>
7+
<style>
8+
body {
9+
margin: 0;
10+
padding: 0;
11+
font-family: Trebuchet MS;
12+
display:flex;
13+
flex-direction: column;
14+
text-align: center;
15+
background-color:#F2C166;
16+
}
17+
header, main, footer {
18+
max-width: 1200px;
19+
margin-left: auto;
20+
margin-right: auto;
21+
}
22+
23+
h2 {
24+
margin-left: auto;
25+
margin-right: auto;
26+
display: block;
27+
max-width: 900px;
28+
}
29+
30+
h1, h2, h3, p {
31+
color:#A60321;
32+
}
33+
34+
hr {
35+
background-color:#A60321;
36+
border: 0px;
37+
height: 2.3px;
38+
border-radius: 3px;
39+
}
40+
841

9-
<link rel="apple-touch-icon" href="apple-touch-icon.png">
42+
</style>
1043
</head>
1144
<body>
12-
<div class="container">
13-
<div class="row">
14-
<h1>What's the difference between .map & forEach</h1>
15-
<h1>Please check the console</h1>
16-
</div>
17-
</div>
18-
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
19-
<script src="/app.js"></script>
45+
<header>
46+
<h1>What's the difference between map() and forEach()</h1>
47+
<hr />
48+
</header>
49+
<main>
50+
<h2>If this is the first you are heading about these array methods. Let me give you a quick defintion of each of these methods and their purpose, before we dig a little deeper.</h2>
51+
<br />
52+
<br />
53+
<h2 style="text-align: left;">From <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map">The Mozilla Documentation</a>, <span class="seoSummary">The <code><strong>map()</strong></code> method <strong>creates a new array</strong> populated with the results of calling a provided function on every element in the calling array.</span></h2>
54+
<br />
55+
<h2 style="text-align: left;">From <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map">The Mozilla Documentation</a>, <span>The <code><strong>forEach()</strong></code> method executes a provided function once for each array element.</span></h2>
56+
<br />
57+
<br />
58+
<h2>These definitions aren't great, simply put both these <u>methods execute a call back function on each item in an array.</u></h2>
59+
<br />
60+
<h2> So, to understand the mechanics of these array methods we start first have to start with an array.</h2>
61+
<iframe width="100%" height="100" src="//jsfiddle.net/ptums/09we13qd/embedded/js/dark" allowfullscreen="allowfullscreen" allowpaymentrequest frameborder="0"></iframe>
62+
<br />
63+
<h2> Now let's run each method on the array as followed...</h2>
64+
<br />
65+
<h2> Here's the <code><strong>map()</strong></code> method in action</h2>
66+
<iframe width="100%" height="400" src="//jsfiddle.net/ptums/awygf57m/34/embedded/js/dark/" allowfullscreen="allowfullscreen" allowpaymentrequest frameborder="0"></iframe>
67+
68+
<br />
69+
<h2> Here's the <code><strong>forEach()</strong></code> method in action</h2>
70+
<iframe width="100%" height="400" src="//jsfiddle.net/ptums/qkowza93/7/embedded/js/dark/" allowfullscreen="allowfullscreen" allowpaymentrequest frameborder="0"></iframe>
71+
72+
<h2>Upon basic execution, both <code><strong>map()</strong></code> and <code><strong>forEach()</strong></code> return the same results and perform the same task.</h2>
73+
<br />
74+
75+
<h2>Now it's time to go a little deeper...</h2>
76+
<br />
77+
<h2>So both both <code><strong>map()</strong></code> and <code><strong>forEach()</strong></code> provide three parameter variables in their call back function. I've named them <code>item, index, array</code>, but feel free to give them any name you <small><u>(pro tip: refer to the MDN documentation for the proper naming convetions)</u></small>.</h2>
78+
<h2>We've seen the <code>item</code> variable in action, now lets look at the <code>index</code> variable for these methods.</h2>
79+
80+
<h2> Here's the <code><strong>map()</strong></code> method in action returing it's <code>index</code> variable parameter</h2>
81+
<iframe width="100%" height="250" src="//jsfiddle.net/ptums/oxfnc8ys/embedded/js/dark/" allowfullscreen="allowfullscreen" allowpaymentrequest frameborder="0"></iframe>
82+
83+
<h2> And, here's the <code><strong>forEach()</strong></code> method in action returing it's <code>index</code> variable parameter</h2>
84+
<iframe width="100%" height="400" src="//jsfiddle.net/ptums/6e0b48p1/7/embedded/js/dark/" allowfullscreen="allowfullscreen" allowpaymentrequest frameborder="0"></iframe>
85+
<br />
86+
<h2>Same results...</h2>
87+
<h2> Here's the <code><strong>map()</strong></code> method in action returing it's <code>array</code> variable parameter</h2>
88+
<iframe width="100%" height="400" src="//jsfiddle.net/ptums/L3xjqbtd/1/embedded/js,css/dark/" allowfullscreen="allowfullscreen" allowpaymentrequest frameborder="0"></iframe>
89+
90+
<h2> And, here's the <code><strong>forEach()</strong></code> method in action returing it's <code>array</code> variable parameter</h2>
91+
<iframe width="100%" height="400" src="//jsfiddle.net/ptums/z0d5arfe/3/embedded/js/dark/" allowfullscreen="allowfullscreen" allowpaymentrequest frameborder="0"></iframe>
92+
<h2>Again, they return the same results...</h2>
93+
<h2>Say, what's going on here? Why do we have two methods that so far do exactly the same thing?</h2>
94+
<br />
95+
<h2>Let's keep digging...</h2>
96+
<br />
97+
<h2>At this point, I'll say about these methods its not what they do that makes them different, but its how they are used that seperates them.</h2>
98+
99+
<h2>Let's explore</h2>
100+
101+
<h2>So we looked at all these parameter variables <code>item, index, array</code> right?</h2>
102+
103+
<h2>Welp, another similarty is that both those variables go the way of the dodo when we want to use a custom callback function.</h2>
104+
<iframe width="100%" height="500" src="//jsfiddle.net/ptums/0m569pwL/4/embedded/js/dark/" allowfullscreen="allowfullscreen" allowpaymentrequest frameborder="0"></iframe>
105+
106+
<h2>So here comes the difference, lets try another call back function that modifies specifically each element in the array.</h2>
107+
108+
<iframe width="100%" height="700" src="//jsfiddle.net/ptums/m0vatnj9/1/embedded/js/dark/" allowfullscreen="allowfullscreen" allowpaymentrequest frameborder="0"></iframe>
109+
110+
<h2>Why <code><strong>forEach()</strong></code> returns <code>undefined</code> is because by nature this method restricts any modification to the elements inside of the array. The method only allows <code>read only</code> access to the items in the array.</h2>
111+
112+
<h2>Here's another example. Lets see what happens when we set each of these methods to a variables</h2>
113+
<iframe width="100%" height="500" src="//jsfiddle.net/ptums/0r6245xa/3/embedded/js/dark/" allowfullscreen="allowfullscreen" allowpaymentrequest frameborder="0"></iframe>
20114

115+
<h2>The reason <code><strong>forEach()</strong></code> returns <code>undefined</code> when set to a variable is because it executes a call back once on each item to an array.</h2>
116+
<h2>So there is a conflict of usage when setting that callback execution to a variable. The browser doesn't know how to interpret one variable set to the results of a callback on each item. </h2>
117+
<h2>Unlike <code><strong>forEach()</strong></code>, the <code><strong>map()</strong></code> method creates a new array on execution, so setting it to a variable will simply return the items recreated as a new array.</h2>
118+
<h2>As of right now, that's all I have to say about these array methods by making a comparission I hope I've demostrated how they functio and there purpose on a deeper level.</h2>
119+
<h2>If you have any question feel free to <a href="https://github.com/ptums/using-javascript-map-foreach-method">make a pull request</a> on this repo and commit your questions.</h2>
120+
<h2>This site is also suplemental material for an article I wrote on the subject called <a href="https://medium.com/@petertumulty/the-power-of-the-map-method-4db6b1a73655">The power of the map method
121+
</a></h2>
122+
<br />
123+
</main>
21124
</body>
22-
</html>
125+
</html>

0 commit comments

Comments
 (0)