The reporting desk needs to summarize employee records. Run script.js and inspect the Console; the three array methods here accept callback functions, building on the previous lessons.
These three methods are the workhorses of functional JavaScript. Chain them together for expressive data pipelines.
const orders = [
{ product: "Book", price: 12, qty: 2 },
{ product: "Pen", price: 1, qty: 10 },
{ product: "Bag", price: 35, qty: 1 }
];
const total = orders
.filter(o => o.qty > 1) // keep multiple-quantity orders
.map(o => o.price * o.qty) // calculate line total
.reduce((sum, lineTotal) => sum + lineTotal, 0); // sum up
console.log(total); // 34 (12*2 + 1*10 = 24 + 10)
engineeringNames with filter and map to retain the Engineering names.avgEngineerSalary with filter and reduce, rounded to the nearest integer.salaryReport for all employees with map and toLocaleString() for salaries.map transforms — returns a new array of the same length.
filter selects — returns a new array that is shorter or equal.
reduce folds — returns a single value (not an array).