-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
244 lines (219 loc) · 8.13 KB
/
scripts.js
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
const employees = [
{
"code": "CT7207",
"salary": 40000,
"id": 1007,
"job": "Manager",
"department": "Operations",
"name": "Bently Smith",
"hobbies": ["Sports", "Reading", "Painting"]
},
{
"code": "CT7210",
"salary": 80000,
"id": 1010,
"job": "Director",
"department": "Operations",
"name": "Isla Morris",
"hobbies": ["Sports", "Reading"]
},
{
"code": "CT7202",
"salary": 15000,
"id": 1002,
"job": "Salesman",
"department": "Sales",
"name": "Allen Green",
"hobbies": ["Sports", "Painting"]
},
{
"code": "CT7208",
"salary": 60000,
"id": 1008,
"job": "Analyst",
"department": "Research",
"name": "Xavier Campbell",
"hobbies": ["Reading", "Painting"]
},
{
"code": "CT7209",
"salary": 50000,
"id": 1009,
"job": "Analyst",
"department": "Research",
"name": "Ethan Kumar",
"hobbies": ["Crafting", "Painting"]
},
{
"code": "CT7201",
"salary": 20000,
"id": 1001,
"job": "Clerk",
"department": "Accounting",
"name": "John Marshal",
"hobbies": ["Singing", "Painting"]
},
{
"code": "CT7205",
"salary": 15000,
"id": 1005,
"job": "Salesman",
"department": "Sales",
"name": "Ethan Almaas",
"hobbies": ["Singing", "Dancing"]
},
{
"code": "CT7211",
"salary": 15000,
"id": 1011,
"job": "Salesman",
"department": "Sales",
"name": "Natalie Robinson",
"hobbies": ["Writing"]
},
{
"code": "CT7212",
"salary": 15000,
"id": 1012,
"job": "Salesman",
"department": "Sales",
"name": "Earl Rose",
"hobbies": ["Singing", "Sports"]
},
{
"code": "CT7206",
"salary": 20000,
"id": 1006,
"job": "Clerk",
"department": "Accounting",
"name": "Ilija Seifert",
"hobbies": ["Singing", "Cooking"]
},
{
"code": "CT7204",
"salary": 20000,
"id": 1004,
"job": "Clerk",
"department": "Accounting",
"name": "Annette Burke",
"hobbies": ["Reading", "Teaching"]
},
{
"code": "CT7203",
"salary": 15000,
"id": 1003,
"job": "Salesman",
"department": "Sales",
"name": "Fernando Gordon",
"hobbies": []
},
{
"code": "CT7213",
"salary": 15000,
"id": 1013,
"job": "Salesman",
"department": "Sales",
"name": "Catherine Foster",
"hobbies": []
}
];
// get employee with id 1003
let result = employees.find(i => i.id === 1003);
console.log('get employee with id 1003:', result);
// get index of an employee having id 1008
result = employees.findIndex(i => i.id === 1008);
console.log('get index of an employee having id 1008:', result);
// get salesman employees
result = employees.filter(i => i.job === 'Salesman');
console.log('get salesman employees:', result);
// get employees having salary greater than 40K
result = employees.filter(i => i.salary > 40000);
console.log('get employees having salary greater than 40K:', result);
// get number of employees having salary greater than 50K and from research department
result = employees.filter(i => i.salary > 50000 && i.department === 'Research');
console.log('get employees having salary greater than 50K and from research department:', result);
// get only name and salary of all employees
result = employees.map(i => ({ name: i.name, salary: i.salary }));
console.log('get only name and salary of all employees:', result);
// get only name, job and annual salary of all employees
result = employees.map(i => ({ name: i.name, job: i.job, annual: i.salary * 12 }));
console.log('get only name, job and annual salary of all employees:', result);
// get only first name and last name of all employees
result = employees.map(i => {
const names = i.name.split(' ');
return { firstName: names[0], lastName: names[1] };
});
console.log('get only first name and last name of all employees:', result);
// get name and salary of employees having salary greater than 10K but less than 20K ordered by name
result = employees
.filter(i => i.salary > 10000 && i.salary < 20000)
.map(i => ({ name: i.name, salary: i.salary }))
.sort((emp1, emp2) => {
const name1 = emp1.name.toLowerCase();
const name2 = emp2.name.toLowerCase();
if (name1 < name2) {
return -1;
} else if (name1 > name2)
return 1;
else return 0;
});
console.log('get name and salary of employees having salary greater than 10K but less than 20K ordered by name:', result);
// return true if all employees having salary greater than 10K
// result = employees.filter(i => i.salary > 10000).length === employees.length;
result = employees.every(i => i.salary > 10000);
console.log('return true if all employees having salary greater than 10K:', result);
// return true if any employee is having salary greater than 60K
// result = employees.filter(i => i.salary > 60000).length > 0;
result = employees.some(i => i.salary > 60000);
console.log('return true if any employee is having salary greater than 60K:', result);
// get employees whose hobby is Reading
// result = employees.filter(i => i.hobbies.filter(hobby => hobby === 'Reading').length);
result = employees.filter(i => i.hobbies.some(hobby => hobby === 'Reading'));
console.log('get employees whose hobby is Reading:', result);
// get sum of all salaries
result = employees.map(i => i.salary).reduce((accumulator, currentValue) => currentValue + accumulator, 0);
console.log('get sum of all salaries:', result);
// get average of salary of employees
result = employees.map(i => i.salary).reduce((accumulator, currentValue) => currentValue + accumulator) / employees.length;
console.log('get average of salary of employees:', result);
// get max salary
result = Math.max(...employees.map(i => i.salary));
console.log('get max salary:', result);
// get min salary
result = Math.min(...employees.map(i => i.salary));
console.log('get min salary:', result);
// get all distinct job names
result = Array.from(new Set(employees.map(i => i.job)));
console.log('get all distinct job names:', result);
// get distinct hobbies
// result = Array.from(new Set(employees.map(i => i.hobbies).flat()));
result = Array.from(new Set(employees.flatMap(i => i.hobbies)));
console.log('get distinct hobbies:', result);
// get employee name and likes (hobbies).
// The likes is a string as "Like hobby1/hobby2/hobby3". The likes will be created from hobbies array
result = employees.map(i => ({ name: i.name, likes: 'Likes ' + i.hobbies.join('/') }));
console.log('get employee name and likes:', result);
// remove employee having id 1010
const employeeIndex = employees.findIndex(i => i.id === 1010);
result = employees.splice(employeeIndex, 1);
console.log('remove employee having id 1010:', result);
// get sum of salary department wise
result = Array
.from(new Set(employees.map(i => i.department)))
.map(department => ({
department,
salary: employees
.filter(i => i.department === department)
.map(i => i.salary)
.reduce((accumulator, currentValue) => currentValue + accumulator, 0)
}));
console.log('get sum of salary department wise:', result);
// get employees name and code whose name starts with alphabet "e" (case insensitive)
result = employees.filter(i => i.name.toLowerCase().startsWith('e'));
console.log('get employees name and code whose name starts with alphabet "e":', result);
// get employees name and code whose name ends with alphabet "n" (case insensitive)
result = employees.filter(i => i.name.toLowerCase().endsWith('n'));
console.log('get employees name and code whose name ends with alphabet "n":', result);
// get employees name and code whose name have "tly"
result = employees.filter(i => i.name.toLowerCase().search('tly') !== -1);
console.log('get employees name and code whose name have "tly":', result);