Функціональні методи масивів — не мутують оригінал (крім forEach).
1forEach — виконує функцію для кожного елемента, не повертає нічого:
2[1, 2, 3].forEach((item, index) => console.log(`${index}: ${item}`));
3map — перетворює кожен елемент, повертає новий масив:
4class="hl-keyword">const doubled = [1, 2, 3].map(x => x * 2); class="hl-comment">// [2, 4, 6]
5filter — залишає тільки ті що проходять умову:
6class="hl-keyword">const evens = [1,2,3,4,5].filter(x => x % 2 === 0); class="hl-comment">// [2, 4]
7reduce — зводить масив до одного значення:const prices = [120, 450, 89, 1200, 300, 75];
console.log(prices.map(p => p * 0.8));
console.log(prices.filter(p => p >= 100 && p <= 500));
const scores = [85, 92, 78, 95, 88];
const sum = scores.reduce((acc, x) => acc + x, 0);
console.log(sum);
console.log((sum / scores.length).toFixed(1));
console.log(Math.max(...scores));
const students = [{name:'Оля',grade:4},{name:'Іван',grade:2},{name:'Петро',grade:5},{name:'Марія',grade:3},{name:'Дмитро',grade:5}];
const result = students
.filter(s => s.grade >= 4)
.map(s => s.name)
.sort();
console.log(result);