Skip to content

Commit a16391f

Browse files
author
zaki.shaikh
committed
first commit
0 parents  commit a16391f

File tree

5 files changed

+291
-0
lines changed

5 files changed

+291
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# JavaScript array methods for living
2+
3+
Check out the CodeOmelet blog post for this project.
4+
5+
Link: http://codeomelet.com/posts/javascript-array-methods-for-living

index.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>JavaScript Array Methods</title>
8+
</head>
9+
10+
<body>
11+
<h1>JavaScript Array Methods</h1>
12+
<script src="scripts.js"></script>
13+
</body>
14+
15+
</html>

package-lock.json

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "array-methods",
3+
"version": "1.0.0",
4+
"description": "JavaScript array methods for living",
5+
"main": "scripts.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/ZakiMohammed/javascript-array-methods.git"
12+
},
13+
"keywords": [
14+
"JavaScript",
15+
"array",
16+
"methods"
17+
],
18+
"author": "Zaki Mohammed",
19+
"license": "ISC",
20+
"bugs": {
21+
"url": "https://github.com/ZakiMohammed/javascript-array-methods/issues"
22+
},
23+
"homepage": "https://github.com/ZakiMohammed/javascript-array-methods#readme"
24+
}

scripts.js

+244
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
const employees = [
2+
{
3+
"code": "CT7207",
4+
"salary": 40000,
5+
"id": 1007,
6+
"job": "Manager",
7+
"department": "Operations",
8+
"name": "Bently Smith",
9+
"hobbies": ["Sports", "Reading", "Painting"]
10+
},
11+
{
12+
"code": "CT7210",
13+
"salary": 80000,
14+
"id": 1010,
15+
"job": "Director",
16+
"department": "Operations",
17+
"name": "Isla Morris",
18+
"hobbies": ["Sports", "Reading"]
19+
},
20+
{
21+
"code": "CT7202",
22+
"salary": 15000,
23+
"id": 1002,
24+
"job": "Salesman",
25+
"department": "Sales",
26+
"name": "Allen Green",
27+
"hobbies": ["Sports", "Painting"]
28+
},
29+
{
30+
"code": "CT7208",
31+
"salary": 60000,
32+
"id": 1008,
33+
"job": "Analyst",
34+
"department": "Research",
35+
"name": "Xavier Campbell",
36+
"hobbies": ["Reading", "Painting"]
37+
},
38+
{
39+
"code": "CT7209",
40+
"salary": 50000,
41+
"id": 1009,
42+
"job": "Analyst",
43+
"department": "Research",
44+
"name": "Ethan Kumar",
45+
"hobbies": ["Crafting", "Painting"]
46+
},
47+
{
48+
"code": "CT7201",
49+
"salary": 20000,
50+
"id": 1001,
51+
"job": "Clerk",
52+
"department": "Accounting",
53+
"name": "John Marshal",
54+
"hobbies": ["Singing", "Painting"]
55+
},
56+
{
57+
"code": "CT7205",
58+
"salary": 15000,
59+
"id": 1005,
60+
"job": "Salesman",
61+
"department": "Sales",
62+
"name": "Ethan Almaas",
63+
"hobbies": ["Singing", "Dancing"]
64+
},
65+
{
66+
"code": "CT7211",
67+
"salary": 15000,
68+
"id": 1011,
69+
"job": "Salesman",
70+
"department": "Sales",
71+
"name": "Natalie Robinson",
72+
"hobbies": ["Writing"]
73+
},
74+
{
75+
"code": "CT7212",
76+
"salary": 15000,
77+
"id": 1012,
78+
"job": "Salesman",
79+
"department": "Sales",
80+
"name": "Earl Rose",
81+
"hobbies": ["Singing", "Sports"]
82+
},
83+
{
84+
"code": "CT7206",
85+
"salary": 20000,
86+
"id": 1006,
87+
"job": "Clerk",
88+
"department": "Accounting",
89+
"name": "Ilija Seifert",
90+
"hobbies": ["Singing", "Cooking"]
91+
},
92+
{
93+
"code": "CT7204",
94+
"salary": 20000,
95+
"id": 1004,
96+
"job": "Clerk",
97+
"department": "Accounting",
98+
"name": "Annette Burke",
99+
"hobbies": ["Reading", "Teaching"]
100+
},
101+
{
102+
"code": "CT7203",
103+
"salary": 15000,
104+
"id": 1003,
105+
"job": "Salesman",
106+
"department": "Sales",
107+
"name": "Fernando Gordon",
108+
"hobbies": []
109+
},
110+
{
111+
"code": "CT7213",
112+
"salary": 15000,
113+
"id": 1013,
114+
"job": "Salesman",
115+
"department": "Sales",
116+
"name": "Catherine Foster",
117+
"hobbies": []
118+
}
119+
];
120+
121+
// get employee with id 1003
122+
let result = employees.find(i => i.id === 1003);
123+
console.log('get employee with id 1003:', result);
124+
125+
// get index of an employee having id 1008
126+
result = employees.findIndex(i => i.id === 1008);
127+
console.log('get index of an employee having id 1008:', result);
128+
129+
// get salesman employees
130+
result = employees.filter(i => i.job === 'Salesman');
131+
console.log('get salesman employees:', result);
132+
133+
// get employees having salary greater than 40K
134+
result = employees.filter(i => i.salary > 40000);
135+
console.log('get employees having salary greater than 40K:', result);
136+
137+
// get number of employees having salary greater than 50K and from research department
138+
result = employees.filter(i => i.salary > 50000 && i.department === 'Research');
139+
console.log('get employees having salary greater than 50K and from research department:', result);
140+
141+
// get only name and salary of all employees
142+
result = employees.map(i => ({ name: i.name, salary: i.salary }));
143+
console.log('get only name and salary of all employees:', result);
144+
145+
// get only name, job and annual salary of all employees
146+
result = employees.map(i => ({ name: i.name, job: i.job, annual: i.salary * 12 }));
147+
console.log('get only name, job and annual salary of all employees:', result);
148+
149+
// get only first name and last name of all employees
150+
result = employees.map(i => {
151+
const names = i.name.split(' ');
152+
return { firstName: names[0], lastName: names[1] };
153+
});
154+
console.log('get only first name and last name of all employees:', result);
155+
156+
// get name and salary of employees having salary greater than 10K but less than 20K ordered by name
157+
result = employees
158+
.filter(i => i.salary > 10000 && i.salary < 20000)
159+
.map(i => ({ name: i.name, salary: i.salary }))
160+
.sort((emp1, emp2) => {
161+
const name1 = emp1.name.toLowerCase();
162+
const name2 = emp2.name.toLowerCase();
163+
164+
if (name1 < name2) {
165+
return -1;
166+
} else if (name1 > name2)
167+
return 1;
168+
else return 0;
169+
});
170+
console.log('get name and salary of employees having salary greater than 10K but less than 20K ordered by name:', result);
171+
172+
// return true if all employees having salary greater than 10K
173+
// result = employees.filter(i => i.salary > 10000).length === employees.length;
174+
result = employees.every(i => i.salary > 10000);
175+
console.log('return true if all employees having salary greater than 10K:', result);
176+
177+
// return true if any employee is having salary greater than 60K
178+
// result = employees.filter(i => i.salary > 60000).length > 0;
179+
result = employees.some(i => i.salary > 60000);
180+
console.log('return true if any employee is having salary greater than 60K:', result);
181+
182+
// get employees whose hobby is Reading
183+
// result = employees.filter(i => i.hobbies.filter(hobby => hobby === 'Reading').length);
184+
result = employees.filter(i => i.hobbies.some(hobby => hobby === 'Reading'));
185+
console.log('get employees whose hobby is Reading:', result);
186+
187+
// get sum of all salaries
188+
result = employees.map(i => i.salary).reduce((accumulator, currentValue) => currentValue + accumulator, 0);
189+
console.log('get sum of all salaries:', result);
190+
191+
// get average of salary of employees
192+
result = employees.map(i => i.salary).reduce((accumulator, currentValue) => currentValue + accumulator) / employees.length;
193+
console.log('get average of salary of employees:', result);
194+
195+
// get max salary
196+
result = Math.max(...employees.map(i => i.salary));
197+
console.log('get max salary:', result);
198+
199+
// get min salary
200+
result = Math.min(...employees.map(i => i.salary));
201+
console.log('get min salary:', result);
202+
203+
// get all distinct job names
204+
result = Array.from(new Set(employees.map(i => i.job)));
205+
console.log('get all distinct job names:', result);
206+
207+
// get distinct hobbies
208+
// result = Array.from(new Set(employees.map(i => i.hobbies).flat()));
209+
result = Array.from(new Set(employees.flatMap(i => i.hobbies)));
210+
console.log('get distinct hobbies:', result);
211+
212+
// get employee name and likes (hobbies).
213+
// The likes is a string as "Like hobby1/hobby2/hobby3". The likes will be created from hobbies array
214+
result = employees.map(i => ({ name: i.name, likes: 'Likes ' + i.hobbies.join('/') }));
215+
console.log('get employee name and likes:', result);
216+
217+
// remove employee having id 1010
218+
const employeeIndex = employees.findIndex(i => i.id === 1010);
219+
result = employees.splice(employeeIndex, 1);
220+
console.log('remove employee having id 1010:', result);
221+
222+
// get sum of salary department wise
223+
result = Array
224+
.from(new Set(employees.map(i => i.department)))
225+
.map(department => ({
226+
department,
227+
salary: employees
228+
.filter(i => i.department === department)
229+
.map(i => i.salary)
230+
.reduce((accumulator, currentValue) => currentValue + accumulator, 0)
231+
}));
232+
console.log('get sum of salary department wise:', result);
233+
234+
// get employees name and code whose name starts with alphabet "e" (case insensitive)
235+
result = employees.filter(i => i.name.toLowerCase().startsWith('e'));
236+
console.log('get employees name and code whose name starts with alphabet "e":', result);
237+
238+
// get employees name and code whose name ends with alphabet "n" (case insensitive)
239+
result = employees.filter(i => i.name.toLowerCase().endsWith('n'));
240+
console.log('get employees name and code whose name ends with alphabet "n":', result);
241+
242+
// get employees name and code whose name have "tly"
243+
result = employees.filter(i => i.name.toLowerCase().search('tly') !== -1);
244+
console.log('get employees name and code whose name have "tly":', result);

0 commit comments

Comments
 (0)