-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlesson6.js
167 lines (132 loc) · 5.5 KB
/
lesson6.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
const students = [
{
name: "Bob",
age: 22,
isMarried: true,
scores: 85
},
{
name: "Alex",
age: 21,
isMarried: true,
scores: 90
},
{
name: "Nick",
age: 20,
isMarried: false,
scores: 120
},
{
name: "John",
age: 19,
isMarried: false,
scores: 100
},
{
name: "Helen",
age: 20,
isMarried: false,
scores: 110
},
{
name: "Ann",
age: 20,
isMarried: false,
scores: 105
},
];
const user = {
name: "Bob",
age: 23,
friends: ["Alex", "Nick", "John"]
}
//1. Создайте поверхностную копию объекта user
let copyUser = {...user}
// //Проверка:
console.log(user === copyUser)
console.log(user.friends === copyUser.friends)
//2. Полная (глубокая) копия объекта user
let deepCopyUser = {...user, friends: [...user.friends]}
// //Проверка:
console.log(user === deepCopyUser)
console.log(user.friends === deepCopyUser.friends)
//3. Поверхностная копия массива students
let copyStudents = [...students]
//Проверка:
console.log(copyStudents === students)
console.log(copyStudents[0] === students[0])
//4*. Полная (глубокая) копия массива students (map)
let deepCopyStudents = students.map(a => ({...a}))
//Проверка:
console.log(deepCopyStudents === students)
console.log(deepCopyStudents[0] === students[0])
// NB!!! Далее все преобразования выполняем не модифицируя исходный массив students
// Вывод результатов - в консоль
//5. Отсортируйте deepCopyStudents по алфавиту (sort)
let sortByName = deepCopyStudents.sort((a, b) => a.name < b.name ? -1 : 1);
console.log(sortByName);
//5a. Отсортируйте deepCopyStudents по успеваемости(лучший идёт первым)(sort)
let sortByScores = deepCopyStudents.sort((a, b) => a.scores < b.scores ? 1 : -1);
console.log(sortByScores);
//6. Сформируйте массив студентов, у которых 100 и более баллов (filter)
let bestStudents = deepCopyStudents.filter(a => a.scores > 100);
console.log(bestStudents)
//6a. Получите массив ("вырежьте") из трёх лучших студентов из массива deepCopyStudents (splice)
//https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
let topStudents = deepCopyStudents.splice(0, 3)
console.log(topStudents)
console.log(deepCopyStudents)
//6b. Объедините массивы deepCopyStudents и topStudents так,
// чтоб сохранился порядок сортировки (spread-оператор )
let newDeepCopyStudents = [...topStudents, ...deepCopyStudents];
console.log(newDeepCopyStudents)
//7. Сформируйте массив холостых студентов (filter)
let notMarriedStudents = newDeepCopyStudents.filter(a => !a.isMarried);
console.log(notMarriedStudents)
//8. Сформируйте массив имён студентов (map)
let studentsNames = newDeepCopyStudents.map(a => a.name);
console.log(studentsNames)
//8a. Сформируйте строку из имён студентов, разделённых
// - запятой (join)
// - пробелом (join)
let nameWithSpace = studentsNames.join(',');
console.log(nameWithSpace)
let namesWithComma = studentsNames.join(' ');
console.log(namesWithComma)
//9. Добавьте всем студентам свойство "isStudent" со значением true (map)
let trueStudents = newDeepCopyStudents.map(a => ({...a, isStudent: true}));
console.log(trueStudents)
//10. Nick женился. Выполните выполните соответствующие преобразование массива students (map)
let studentsWithMarriedNick = newDeepCopyStudents.map(a => {
// if (a.name === 'Nick'){
// return {...a,isMarried: true}
// } else {
// return {...a}
// }
return (a.name === 'Nick') ? {...a, isMarried: true} : {...a}
});
console.log(studentsWithMarriedNick)
//11. Найдите студента по имени Ann (find)
let ann = newDeepCopyStudents.find(a => a.name === 'Ann');
console.log(ann)
//12. Найдите студента с самым высоким баллом (reduce)
let bestStudent = newDeepCopyStudents.reduce((acc, el) => {
if (el.scores > acc) {
acc = el
}
return acc
}, 0);
console.log(bestStudent)
//13. Найдите сумму баллов всех студентов (reduce)
let scoresSum = newDeepCopyStudents.reduce((a, b) => a + b.scores, 0);
console.log(scoresSum)
// 14.Напишите функцию addFriends, которая принимает параметром массив students
// и добавляет в каждому студенту свойство "friends",
// значением которого является массив имён всех остальных студентов из массива,
// за исключением собственного имени студента. Т.е. в друзьях у Боба Боба быть не должно.
const addFriends = (students) => {
let arr = students.map(a => a.name)
return students.map(a => ({...a, friends: arr.filter(b => b !== a.name)}))
}
console.log(addFriends(students));