-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew2.js
More file actions
114 lines (84 loc) · 2.58 KB
/
new2.js
File metadata and controls
114 lines (84 loc) · 2.58 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
//High Order function
const employees = [
{id:1, name: 'Ram', salary: 10000, skills: ['JAVA', 'ANGULAR']},
{id:2, name: 'Shyam', salary: 9000, skills: ['Oracle', 'LINUX']}
];
//print all the names of employee
employees.forEach((n)=>console.log(n.name))
//join all names with comma
console.log(employees.map((k)=>k.name).join(', '))
//get total salary of all employees
console.log(employees.map((s)=> s.salary).reduce((p,c)=>p+c,0))
//get employees with salary > 5000
console.log(employees.filter((x)=> x.salary > 5000))
//return true if there is an employee whose salary < 11000
console.log(employees.some((e, i) => {
console.log(`Index at: ${i}`);
return e.salary < 11000;
}));
// find possible skill sets based on the employee array data.
console.log(employees.map((e)=>e.skills).flat());
// flatMap: find possible skill sets based on the employee array data
console.log(employees.flatMap((e) => e.skills));
//find employee having name Ram
console.log(employees.find((e)=> e.name.includes('Ram')));
//sort employee array by salary in ascending order
employees.sort((a,b)=>{
return a.salary - b.salary});
console.log(employees)
//Arrow function
//no parameter
greet = () => console.log("hello");
greet();
//parameter
getName = (name) => console.log(` hello ${name} ` );
getName('Ram');
//ES6 class
class Animal{
constructor(id,name){
this.id = id;
this.name=name;
}
}
class Dog extends Animal{
constructor(id, name, tail, bark){
super(id,name);
this.tail = tail;
this.bark = bark;
}
}
const pet = new Dog(1, "Seti", "wags tail", "maiihoohaii")
const pet2 = new Dog(1, "Seti", "wags tail", ["hoooo", "maiihooohai", "oieRocky"])
console.log(pet);
console.log(pet2);
//constructor function
function Trainee (id, name, batch, preferredSkillSet){
this.id=id;
this.name=name;
this.batch= batch;
this.preferredSkillSet=preferredSkillSet;
}
const trainee1 = new Trainee(1, 'Ram', 'April Batch', ["java", "nodejs"]);
console.log(trainee1)
//arrow function
const location1 = {
city: 'Kathmandu',
country: 'Nepal',
print: () => {
console.log(arguments);
console.log(`${this.city}, ${this.country}`)
}
}
location1.print(123);
//regular fn
const location = {
city: 'Kathmandu',
country: 'Nepal',
print: function () {
console.log(arguments);
console.log(`${this.city}, ${this.country}`);
}
}
location.print(123);
//prototype
//regular vs arrow function