Skip to content

Commit 643d2e1

Browse files
committed
Update readme with class-agnostic examples
1 parent e857418 commit 643d2e1

File tree

1 file changed

+36
-47
lines changed

1 file changed

+36
-47
lines changed

README.md

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,11 @@ Map works by returning a new array after a function has been applied to every si
3737
Imagine we have a group of users with multiple attributes.
3838

3939
```javascript
40-
var users = [{ firstName: 'Bradley', lastName: 'Bouley', role: 'Full Stack Resident' },
41-
{ firstName: 'Chloe', lastName: 'Alnaji', role: 'Full Stack Resident' },
42-
{ firstName: 'Jonathan', lastName: 'Baughn', role: 'Enterprise Instructor' },
43-
{ firstName: 'Michael', lastName: 'Herman', role: 'Lead Instructor' },
44-
{ firstName: 'Robert', lastName: 'Hajek', role: 'Full Stack Resident' },
45-
{ firstName: 'Wes', lastName: 'Reid', role: 'Instructor'},
46-
{ firstName: 'Zach', lastName: 'Klabunde', role: 'Instructor'}];
40+
var users = [{ firstName: 'Homer', lastName: 'Simpson' },
41+
{ firstName: 'Marge', lastName: 'Simpson' },
42+
{ firstName: 'Bart', lastName: 'Simpson' },
43+
{ firstName: 'Lisa', lastName: 'Simpson' },
44+
{ firstName: 'Maggie', lastName: 'Simpson' }];
4745
```
4846

4947
We're going to send out a message to all our users but just need their first name in order to personalize it. We can use `.map()` to quickly return an array of just their first names.
@@ -53,13 +51,13 @@ users.map(function (user) {
5351
return user.firstName;
5452
});
5553

56-
// [ 'Bradley',
57-
// 'Chloe',
58-
// 'Jonathan',
59-
// 'Michael',
60-
// 'Robert',
61-
// 'Wes',
62-
// 'Zach' ]
54+
// [
55+
// 'Homer',
56+
// 'Marge',
57+
// 'Bart',
58+
// 'Lisa',
59+
// 'Maggie'
60+
// ]
6361
```
6462

6563
It's important to take note of the `return` inside of the anonymous function that is passed into `.map()`. Without that, our array of first names will not be quite what we expect.
@@ -69,13 +67,13 @@ users.map(function (user) {
6967
user.firstName;
7068
});
7169

72-
// [ undefined,
70+
// [
7371
// undefined,
7472
// undefined,
7573
// undefined,
7674
// undefined,
77-
// undefined,
78-
// undefined ]
75+
// undefined
76+
// ]
7977
```
8078

8179
Calling `return` is *crucial* when using all of these higher order functions. `.map()` also takes additional arguments, so make sure to check out the [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) Docs!
@@ -93,25 +91,24 @@ Filter allows us to remove elements that don't fit certain criteria. It's incred
9391
Imagine we have a group of users with multiple attributes.
9492

9593
```javascript
96-
var users = [{ firstName: 'Bradley', lastName: 'Bouley', role: 'Full Stack Resident' },
97-
{ firstName: 'Chloe', lastName: 'Alnaji', role: 'Full Stack Resident' },
98-
{ firstName: 'Jonathan', lastName: 'Baughn', role: 'Enterprise Instructor' },
99-
{ firstName: 'Michael', lastName: 'Herman', role: 'Lead Instructor' },
100-
{ firstName: 'Robert', lastName: 'Hajek', role: 'Full Stack Resident' },
101-
{ firstName: 'Wes', lastName: 'Reid', role: 'Instructor'},
102-
{ firstName: 'Zach', lastName: 'Klabunde', role: 'Instructor'}];
94+
var users = [{ character: 'Superman', hero: true },
95+
{ character: 'Sinestro', hero: false },
96+
{ character: 'Wonder Woman', hero: true },
97+
{ character: 'Lex Luthor', hero: false },
98+
{ character: 'Ares', hero: false },
99+
{ character: 'Green Lantern', hero: true }];
103100
```
104101

105-
We want to send out a message to just the Full Stack Residents, telling them what a wonderful job they're doing. We can use `.filter()` to return just those users who fit the right role.
102+
We want to send out a message to just the heroes, telling them what a wonderful job they're doing. We can use `.filter()` to return just those users who fit the right role.
106103

107104
```javascript
108105
users.filter(function (user) {
109-
return user.role === 'Full Stack Resident';
106+
return user.hero;
110107
});
111108

112-
// [{ firstName: 'Bradley', lastName: 'Bouley', role: 'Full Stack Resident' },
113-
// { firstName: 'Chloe', lastName: 'Alnaji', role: 'Full Stack Resident' },
114-
// { firstName: 'Robert', lastName: 'Hajek', role: 'Full Stack Resident' }];
109+
// [{ character: 'Superman', hero: true },
110+
// { character: 'Wonder Woman', hero: true },
111+
// { character: 'Green Lantern', hero: true }]
115112
```
116113

117114
Just like the other functions here, `.filter()` also takes additional arguments, so make sure to check out the [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) Docs!
@@ -163,32 +160,24 @@ epic.reduce(function (previous, current) {
163160
Let's get back to our users and see how we can actually set the initial value for our reduce method!
164161

165162
```javascript
166-
var users = [{ firstName: 'Bradley', lastName: 'Bouley', role: 'Full Stack Resident' },
167-
{ firstName: 'Chloe', lastName: 'Alnaji', role: 'Full Stack Resident' },
168-
{ firstName: 'Jonathan', lastName: 'Baughn', role: 'Enterprise Instructor' },
169-
{ firstName: 'Michael', lastName: 'Herman', role: 'Lead Instructor' },
170-
{ firstName: 'Robert', lastName: 'Hajek', role: 'Full Stack Resident' },
171-
{ firstName: 'Wes', lastName: 'Reid', role: 'Instructor'},
172-
{ firstName: 'Zach', lastName: 'Klabunde', role: 'Instructor'}];
163+
var users = [{ fullName: 'George Washington', email: '[email protected]' },
164+
{ fullName: 'John Adams', email: '[email protected]' },
165+
{ fullName: 'Thomas Jefferson', email: '[email protected]' },
166+
{ fullName: 'James Madison', email: '[email protected]' }];
173167
```
174168

175-
We want to change up the structure of our users so that we can use the users' full name as the key and have their role as the value. Normally, this would take a lot of looping and initializing some variables. However, with reduce we can set an empty object as our starting point (i.e. previous) and do it all in a single go!
169+
We want to change up the structure of our users so that we can use the users' full name as the key and have their email as the value. Normally, this would take a lot of looping and initializing some variables. However, with reduce we can set an empty object as our starting point (i.e. previous) and do it all in a single go!
176170

177171
```javascript
178172
users.reduce(function (usersObj, user) {
179-
var fullName = user.firstName + ' ' + user.lastName;
180-
usersObj[fullName] = user.role;
181-
173+
usersObj[user.fullName] = user.email;
182174
return usersObj;
183175
}, {});
184176

185-
// { 'Bradley Bouley': 'Full Stack Resident',
186-
// 'Chloe Alnaji': 'Full Stack Resident',
187-
// 'Jonathan Baughn': 'Enterprise Instructor',
188-
// 'Michael Herman': 'Lead Instructor',
189-
// 'Robert Hajek': 'Full Stack Resident',
190-
// 'Wes Reid': 'Instructor',
191-
// 'Zach Klabunde': 'Instructor' }
177+
// { 'George Washington': '[email protected]',
178+
// 'John Adams': '[email protected]',
179+
// 'Thomas Jefferson': '[email protected]',
180+
// 'James Madison': '[email protected]' }
192181
```
193182

194183
Notice the empty object as the second argument in reduce, as well as the fact that we're constantly returning our `usersObj` on each iteration.

0 commit comments

Comments
 (0)