2
2
3
3
// have access to students from data.js
4
4
5
- /* map */
5
+ /*
6
+ =============
7
+ map
8
+ =============
9
+ */
6
10
const updatedStudents = students . map ( function ( student ) {
7
11
student . role = 'student' ;
8
12
return student ;
9
13
} ) ;
10
14
// console.log(updatedStudents);
11
15
12
- /* filter */
16
+ /*
17
+ =============
18
+ filter
19
+ =============
20
+ */
13
21
const highScores = students . filter ( function ( student ) {
14
22
// if (student.score >= 80) {
15
23
// return student;
@@ -20,13 +28,21 @@ const highScores = students.filter(function (student) {
20
28
} ) ;
21
29
// console.log(highScores);
22
30
23
- /* find */
31
+ /*
32
+ =============
33
+ find
34
+ =============
35
+ */
24
36
const specificId = students . find ( function ( student ) {
25
37
return student . id === 2 ;
26
38
} ) ;
27
39
// console.log(specificId);
28
40
29
- /* reduce */
41
+ /*
42
+ =============
43
+ reduce
44
+ =============
45
+ */
30
46
const averageScore =
31
47
students . reduce ( function ( totalScore , student ) {
32
48
// console.log(totalScore);
@@ -35,10 +51,14 @@ const averageScore =
35
51
return totalScore ;
36
52
} , 0 ) / students . length ;
37
53
38
- console . log ( averageScore ) ;
54
+ // console.log(averageScore);
39
55
40
- /* Example - Square Bracket Notation */
41
- const subject = 'physics' ;
56
+ /*
57
+ =======================
58
+ Square Bracket Notation
59
+ =======================
60
+ */
61
+ const otherSubject = 'physics' ;
42
62
43
63
const total = { } ;
44
64
@@ -48,6 +68,24 @@ total.history = 1;
48
68
total . arts = 1 ;
49
69
50
70
// Dynamic Property Creation based on variable
51
- total [ subject ] = 'some value' ;
71
+ total [ otherSubject ] = 'some value' ;
52
72
53
- console . log ( total ) ;
73
+ // console.log(total);
74
+
75
+ /*
76
+ =============
77
+ reduce - survey
78
+ =============
79
+ */
80
+ const survey = students . reduce ( function ( survey , student ) {
81
+ const favoriteSubject = student . favoriteSubject ;
82
+ // console.log(favoriteSubject);
83
+ if ( survey [ favoriteSubject ] ) {
84
+ survey [ favoriteSubject ] = survey [ favoriteSubject ] + 1 ;
85
+ } else {
86
+ survey [ favoriteSubject ] = 1 ;
87
+ }
88
+ return survey ;
89
+ } , { } ) ;
90
+
91
+ console . log ( survey ) ;
0 commit comments