-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_iteration2.js
More file actions
189 lines (162 loc) · 4.34 KB
/
Copy patharray_iteration2.js
File metadata and controls
189 lines (162 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// What you have
var officers = [
{ id: 20, name: 'Captain Piett' },
{ id: 24, name: 'General Veers' },
{ id: 56, name: 'Admiral Ozzel' },
{ id: 88, name: 'Commander Jerjerrod' }
];
// What you need
//[20, 24, 56, 88]
console.log('--------------forEach-----------');
var officersId=[];
officers.forEach(function(officer){
officersId.push(officer.id);
});
console.log(officersId);
console.log("=========================");
console.log('--------------map-----------');
var officersId2=officers.map(function(officer){
return officer.id;
});
console.log(officersId2);
var officersId3=officers.map((officer)=>{
return officer.id;
});
console.log(officersId3);
var officersId4=officers.map(officer=>officer.id);
console.log(officersId4);
console.log("=========================");
console.log('--------------reduce-----------');
var pilots = [
{
id: 10,
name: "Poe Dameron",
years: 14,
},
{
id: 2,
name: "Temmin 'Snap' Wexley",
years: 30,
},
{
id: 41,
name: "Tallissan Lintra",
years: 16,
},
{
id: 99,
name: "Ello Asty",
years: 22,
}
];
var totalYears=pilots.reduce(function(accumulator,pilot){
return accumulator+pilot.years;
},0);
console.log("totalYears: " + totalYears);
var totalYears2=pilots.reduce((accumulator,pilot)=>accumulator+pilot.years,0);
console.log("totalYears2: " + totalYears2);
var mostExpPilot = pilots.reduce(function (oldest, pilot) {
return (oldest.years || 0) > pilot.years ? oldest : pilot;
}, {});
console.log("mostExpPilot: " + mostExpPilot);
var oldest=pilots.reduce((oldest,pilot)=>oldest>pilot.years?oldest:pilot.years,0);
console.log("oldest: " + oldest);
console.log("=========================");
console.log('--------------filter-----------');
var pilots = [
{
id: 2,
name: "Wedge Antilles",
faction: "Rebels",
},
{
id: 8,
name: "Ciena Ree",
faction: "Empire",
},
{
id: 40,
name: "Iden Versio",
faction: "Empire",
},
{
id: 66,
name: "Thane Kyrell",
faction: "Rebels",
}
];
var rebels=pilots.filter(pilot=>pilot.faction==="Rebels");
console.log(rebels);
var empires=pilots.filter(pilot=>pilot.faction==="Empire");
console.log(empires);
console.log("=========================");
console.log('--------------all-----------');
var personnel = [
{
id: 5,
name: "Luke Skywalker",
pilotingScore: 98,
shootingScore: 56,
isForceUser: true,
},
{
id: 82,
name: "Sabine Wren",
pilotingScore: 73,
shootingScore: 99,
isForceUser: false,
},
{
id: 22,
name: "Zeb Orellios",
pilotingScore: 20,
shootingScore: 59,
isForceUser: false,
},
{
id: 15,
name: "Ezra Bridger",
pilotingScore: 43,
shootingScore: 67,
isForceUser: true,
},
{
id: 11,
name: "Caleb Dume",
pilotingScore: 71,
shootingScore: 85,
isForceUser: true,
},
];
var forceUser=personnel.filter(person=>person.isForceUser===true);
console.log(forceUser);
var score=forceUser.map(user=>user.pilotingScore+user.shootingScore);
console.log("score: " + score);
var forceUser2=personnel.filter(person=>person.isForceUser);
console.log(forceUser2);
var forceUser3=forceUser.map(user=>user);
console.log(forceUser3);
console.log("=========================");
var totalPilotingScore=personnel.reduce((total,person)=>total+person.pilotingScore,0);
console.log("totalPilotingScore: " + totalPilotingScore);
var totalShootingScore=personnel.reduce((total,person)=>total+person.shootingScore,0);
console.log("totalShootingScore: " + totalShootingScore);
var totalScore=personnel.reduce((total,person)=>total+person.pilotingScore+person.shootingScore,0);
console.log("totalScore: " + totalScore);
var forceUserScore=personnel
.filter(person=>person.isForceUser)
.map(jedi=>jedi.pilotingScore+jedi.shootingScore)
.reduce((total,score)=>total+score,0);
console.log("forceUserScore: " + forceUserScore);
console.log("=========================");
var data = [
{
name: "Jan Dodonna",
title: "General",
},
{
name: "Gial Ackbar",
title: "Admiral",
},
];
console.log(data.map(data1=>data1.title+' : '+data1.name));