Sum employee salaries across nested department objects
You are tasked with implementing a function named calculateTotalSalaries that will navigate through a company’s hierarchical structure to calculate the cumulative sum of salaries for all employees. The company’s structure is depicted as a nested object, where each node can either be a department containing further sub-departments or an array of employees. Each employee is represented by an object containing their name and salary. To compute total salaries across nested departments, recursion is the natural fit.
The main challenge lies in efficiently traversing this nested structure, which may vary in depth and complexity across different companies, to aggregate all the salary values. This problem not only tests your ability to work with nested data structures but also assesses your recursive thinking and problem-solving skills.
Input: A nested object representing the company’s structure, where each department can contain sub-departments or an array of employees with their respective salaries.
Output: A single integer value representing the total sum of salaries of all employees across the entire company.
Example:
const company = {
sales: [
{ name: "John", salary: 1000 },
{ name: "Alice", salary: 1600 }
],
development: {
sites: [
{ name: "Peter", salary: 2000 },
{ name: "Alex", salary: 1800 }
],
internals: [
{ name: "Jack", salary: 1300 }
]
}
};
const totalSalaries = calculateTotalSalaries(company);
console.log(totalSalaries); // Output: 7700
In this example, the function calculateTotalSalaries navigates through each department and sub-department, summing up the salaries of “John”, “Alice”, “Peter”, “Alex”, and “Jack”, resulting in a total of 7700.
This problem encapsulates a real-world scenario where understanding and manipulating nested data structures are crucial. It provides a foundation for more complex data processing tasks that are common in software development, especially in handling hierarchical data.
name (a string) and salary (an integer).