You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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' }];
47
45
```
48
46
49
47
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) {
53
51
returnuser.firstName;
54
52
});
55
53
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
+
// ]
63
61
```
64
62
65
63
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) {
69
67
user.firstName;
70
68
});
71
69
72
-
// [ undefined,
70
+
// [
73
71
// undefined,
74
72
// undefined,
75
73
// undefined,
76
74
// undefined,
77
-
// undefined,
78
-
// undefined ]
75
+
// undefined
76
+
// ]
79
77
```
80
78
81
79
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
93
91
Imagine we have a group of users with multiple attributes.
94
92
95
93
```javascript
96
-
var users = [{ firstName:'Bradley', lastName:'Bouley', role:'Full Stack Resident' },
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.
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!
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!
0 commit comments