javascript map reduce code example
Example 1: map and reduce an array in js
const rebels = pilots.filter(pilot => pilot.faction === "Rebels");const empire = pilots.filter(pilot => pilot.faction === "Empire");
Example 2: js map filter reduce
const numbers = [1, 2, 3, 4];
const doubled = numbers.map(item => item * 2);
console.log(doubled);
Example 3: js map filter reduce
const numbers = [1, 2, 3, 4];
const evens = numbers.filter(item => item % 2 === 0);
console.log(evens);
Example 4: reduce javascript
const initialValue = 0;
const numbers = [5, 10, 15];
const reducer = (accumulator, item) => {
return accumulator + item;
};
const total = numbers.reduce(reducer, initialValue)
Example 5: js map filter reduce
const students = [
{ name: 'Quincy', grade: 96 },
{ name: 'Jason', grade: 84 },
{ name: 'Alexis', grade: 100 },
{ name: 'Sam', grade: 65 },
{ name: 'Katie', grade: 90 }
];
const studentGrades = students.filter(student => student.grade >= 90);
return studentGrades;
Example 6: js map filter reduce
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce(function (result, item) {
return result + item;
}, 0);
console.log(sum);