Goal: to master methods of aggregating and summing data in an array of objects.
This task can be solved in several ways.
Method 1: Using Array.prototype.reduce
This method is ideal for “reducing” an array to a single value, such as an object.
reduce with an empty object {}.Method 2: Using a loop (for...of or forEach)
A classic approach that is easy to read.
result = {}.reduce: check for the department’s existence in result and update the bonus sum.reduceconst sumBonusesByDepartment = (employees) => {
// Start "reducing" the array with an empty object {}.
// `acc` is the accumulator that stores the final object.
// `employee` is the current element being processed.
return employees.reduce((acc, employee) => {
const { department, bonus } = employee;
// If the department is not yet in the accumulator, initialize it.
// Use `|| 0` in case the bonus is not specified.
if (!acc[department]) {
acc[department] = 0;
}
// Add the employee's bonus to the total for their department.
acc[department] += bonus || 0;
// Return the updated accumulator for the next iteration.
return acc;
}, {}); // Initial value of the accumulator.
};Solution Analysis:
for...of loopconst sumBonusesByDepartment = (employees) => {
// Create an empty object to store the results.
const result = {};
// Iterate over each employee in the array.
for (const employee of employees) {
const { department, bonus } = employee;
// If the department is not yet in the result object, create it.
if (!result[department]) {
result[department] = 0;
}
// Add the bonus to the sum for the corresponding department.
result[department] += bonus || 0;
}
// Return the final object.
return result;
};Solution Analysis:
result object.You need to implement the sumBonusesByDepartment function, which takes an array of employees objects. Each object contains information about an employee: name, department, and bonus.
The function should calculate the total sum of bonuses for each department and return an object where the keys are the department names and the values are the total bonus amounts for that department.
const employees = [
{ name: 'John', department: 'Development', bonus: 1000 },
{ name: 'Mary', department: 'Sales', bonus: 1500 },
{ name: 'Peter', department: 'Development', bonus: 1200 },
{ name: 'Ann', department: 'Sales', bonus: 1800 },
{ name: 'Helen', department: 'HR', bonus: 800 }
];
sumBonusesByDepartment(employees);
// Expected result:
// { 'Development': 2200, 'Sales': 3300, 'HR': 800 }sumBonusesByDepartment.{}.bonus field, their bonus should be treated as 0.Goal: to master methods of aggregating and summing data in an array of objects.
This task can be solved in several ways.
Method 1: Using Array.prototype.reduce
This method is ideal for “reducing” an array to a single value, such as an object.
reduce with an empty object {}.Method 2: Using a loop (for...of or forEach)
A classic approach that is easy to read.
result = {}.reduce: check for the department’s existence in result and update the bonus sum.reduceconst sumBonusesByDepartment = (employees) => {
// Start "reducing" the array with an empty object {}.
// `acc` is the accumulator that stores the final object.
// `employee` is the current element being processed.
return employees.reduce((acc, employee) => {
const { department, bonus } = employee;
// If the department is not yet in the accumulator, initialize it.
// Use `|| 0` in case the bonus is not specified.
if (!acc[department]) {
acc[department] = 0;
}
// Add the employee's bonus to the total for their department.
acc[department] += bonus || 0;
// Return the updated accumulator for the next iteration.
return acc;
}, {}); // Initial value of the accumulator.
};Solution Analysis:
for...of loopconst sumBonusesByDepartment = (employees) => {
// Create an empty object to store the results.
const result = {};
// Iterate over each employee in the array.
for (const employee of employees) {
const { department, bonus } = employee;
// If the department is not yet in the result object, create it.
if (!result[department]) {
result[department] = 0;
}
// Add the bonus to the sum for the corresponding department.
result[department] += bonus || 0;
}
// Return the final object.
return result;
};Solution Analysis:
result object.You need to implement the sumBonusesByDepartment function, which takes an array of employees objects. Each object contains information about an employee: name, department, and bonus.
The function should calculate the total sum of bonuses for each department and return an object where the keys are the department names and the values are the total bonus amounts for that department.
const employees = [
{ name: 'John', department: 'Development', bonus: 1000 },
{ name: 'Mary', department: 'Sales', bonus: 1500 },
{ name: 'Peter', department: 'Development', bonus: 1200 },
{ name: 'Ann', department: 'Sales', bonus: 1800 },
{ name: 'Helen', department: 'HR', bonus: 800 }
];
sumBonusesByDepartment(employees);
// Expected result:
// { 'Development': 2200, 'Sales': 3300, 'HR': 800 }sumBonusesByDepartment.{}.bonus field, their bonus should be treated as 0.The code editor is intentionally hidden on mobile.
Believe me, it's for the best: I am protecting you from the temptation to code in less-than-ideal conditions. A small screen and a virtual keyboard are not the best tools for a programmer.
📖 Now: Study the task, think through the solution. Act like a strategist.
💻 Later: Sit down at your computer, open the site, and implement all your ideas comfortably. Act like a code-jedi!