-
Notifications
You must be signed in to change notification settings - Fork 0
/
swit.js
140 lines (107 loc) · 4.26 KB
/
swit.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
console.log("forEach 1 ttttttttttttttttttttttttttt");
const persons = [
{ firstName: "John", lastName: "Doe", age: 32 },
{ firstName: "Jane", lastName: "Doe", age: 23 },
];
let maxAge = persons[0].age; // Assume the first person has the max age
let oldestPerson = persons[0]; // Assume the first person is the oldest
persons.forEach((person) => {
if (person.age > maxAge) {
maxAge = person.age; // Update maxAge if the current person's age is larger
oldestPerson = person; // Update oldestPerson
}
});
console.log(
`The oldest person is: ${oldestPerson.firstName} ${oldestPerson.lastName}, Age: ${oldestPerson.age}`
);
console.log(
" forEach 2 ........................................................"
);
//---------------------------------------------------------------
const persones = [
{ firstName: "John", lastName: "Doe", age: 32 },
{ firstName: "Jane", lastName: "Doe", age: 23 },
];
persones.forEach((person, index) => {
if (index % 2 === 1) {
// Log every second element (index 1, 3, 5, etc.)
console.log(person);
}
});
console.log(
" forEach 3........................................................"
);
//---------------------------------------------------------------
// ß3äääääääääääääääääääääääääääääääääääääääääääääääääääääääää
const perso = [
{ firstName: "John", lastName: "Doe", age: 32 },
{ firstName: "Jane", lastName: "Doe", age: 23 },
];
perso.forEach((perso) => {
console.log(`${perso.firstName} ${perso.lastName}`);
});
console.log("filtermmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm");
// filtermmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
const pers = [
{ firstName: "John", lastName: "Doe", age: 32 },
{ firstName: "Jane", lastName: "Doe", age: 23 },
];
// Using Array.filter() to create a new array with persons under 30
const personsUnder30 = pers.filter((pers) => pers.age < 30);
// Logging the new array
console.log(personsUnder30);
console.log("sort 1 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
// hhhhhhh sort hhhhhhhhhhhhh
// lllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll
const personses = [
{ firstName: "John", lastName: "Doe", age: 32 },
{ firstName: "Jane", lastName: "Doe", age: 23 },
];
// Using Array.sort() to sort the persons array by age in ascending order
const sortedByAge = personses.sort((a, b) => a.age - b.age);
// The maximum value will be the last element of the sorted array
const maxAgePerson = sortedByAge[sortedByAge.length - 1];
// Logging the person with the maximum age
console.log(maxAgePerson);
console.log("sort 2 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
const personsn = [
{ firstName: "John", lastName: "Doe", age: 32 },
{ firstName: "Jane", lastName: "Doe", age: 23 },
];
// Using Array.sort() to create a new array sorted by age in ascending order
const personsSortedByAge = personsn.sort((a, b) => a.age - b.age);
// Logging the sorted array
console.log(personsSortedByAge);
console.log(" MAP _____pppppppppppppppppppppppppppppp");
// input data
const personEs = [
{ firstName: "John", lastName: "Doe", age: 32 },
{ firstName: "Jane", lastName: "Doe", age: 23 },
];
// Using Array.map() to add the fullName property to each object
const personsWithFullName = personEs.map((perSon) => {
return {
...perSon, // Spread the original person object
fullName: `${perSon.firstName} ${perSon.lastName}`, // Add fullName property
};
});
console.log(personsWithFullName);
// Output data should look like:
// [
// { firstName: 'John', lastName: 'Doe', age: 32, fullName: 'John Doe' },
// { firstName: 'Jane', lastName: 'Doe', age: 23, fullName: 'Jane Doe' },
// ]
//…reduce………..,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.
console.log("REDUCE ##############################################");
const personsN = [
{ firstName: "John", lastName: "Doe", age: 32 },
{ firstName: "Jane", lastName: "Doe", age: 23 },
];
// Using Array.reduce() to calculate the sum of ages
const totalAge = personsN.reduce((sum, pErson) => sum + pErson.age, 0);
// Calculating the average age
const averageAge = totalAge / personsN.length;
// Logging the average age
console.log(averageAge);