Recursive Filter and Manipulation on Complex Objects
Sun May 05 2024
Recursive Employee Search:
Implemented a function getAllSoftwareEngineerRoles that traverses the company's hierarchical department structure to find and list all employees with the role "Software Engineer."
Mapping and Filtering:
For each department and its subdepartments, the function uses map and filter to extract employee names based on their role, recursively including results from deeper levels.
Data Aggregation:
Combines all results using flat() to produce a flat array of employee names, showcasing effective data traversal and manipulation across complex nested structures
Recursive Salary Assignment:
Developed addEmployeeSalary to traverse the entire departmental structure, adding a randomized salary to each employee.
Iterative and Recursive Processing:
For each department, iterates through its employees to assign salaries and recursively processes any subdepartments.
1const company = {
2    name: "Tech Solutions",
3    departments: [
4        {
5            name: "Engineering",
6            primaryBenefit: {
7                bonus: "10%",
8                leaves: {
9                    casual: 10,
10                    sick: 5
11                },
12                foundingDay: new Date("2010-04-01")
13            },
14            employees: [
15                { name: "Alice", role: "Software Engineer" },
16                { name: "Bob", role: "DevOps Engineer" }
17            ],
18            subdepartments: [
19                {
20                    name: "Quality Assurance",
21                    employees: [
22                        { name: "Charlie", role: "QA Engineer" },
23                        { name: "Josh", role: "Software Engineer" }
24                    ],
25                    subdepartments: [
26                        {
27                            name: "Automation Testing",
28                            employees: [
29                                { name: "Kim", role: "Automation Engineer" }
30                            ],
31                            subdepartments: []
32                        },
33                        {
34                            name: "Manual Testing",
35                            employees: [
36                                { name: "Lara", role: "Manual Tester" }
37                            ],
38                            subdepartments: []
39                        }
40                    ]
41                },
42                {
43                    name: "Development",
44                    employees: [
45                        { name: "Sara", role: "Backend Developer" },
46                        { name: "Mike", role: "Frontend Developer" }
47                    ],
48                    subdepartments: [
49                        {
50                            name: "UI/UX",
51                            employees: [
52                                { name: "Anne", role: "UI Designer" },
53                                { name: "Sam", role: "UX Researcher" }
54                            ],
55                            subdepartments: []
56                        }
57                    ]
58                }
59            ]
60        },
61        {
62            name: "Human Resources",
63            primaryBenefit: {
64                bonus: "12%",
65                leaves: {
66                    casual: 12,
67                    sick: 6
68                },
69                foundingDay: new Date("2012-06-15")
70            },
71            employees: [
72                { name: "David", role: "Recruiter" },
73                { name: "Eve", role: "Benefits Coordinator" }
74            ],
75            subdepartments: [
76                {
77                    name: "Employee Relations",
78                    employees: [
79                        { name: "Tina", role: "Employee Relations Specialist" },
80                        { name: "Rob", role: "Workplace Investigator" }
81                    ],
82                    subdepartments: []
83                }
84            ]
85        },
86        {
87            name: "Marketing",
88            primaryBenefit: {
89                bonus: "15%",
90                leaves: {
91                    casual: 15,
92                    sick: 7
93                },
94                foundingDay: new Date("2015-08-30")
95            },
96            employees: [
97                { name: "Lucy", role: "Marketing Director" },
98                { name: "Ethan", role: "Content Strategist" }
99            ],
100            subdepartments: [
101                {
102                    name: "Digital Marketing",
103                    employees: [
104                        { name: "Omar", role: "SEO Specialist" },
105                        { name: "Jade", role: "Social Media Manager" }
106                    ],
107                    subdepartments: [
108                        {
109                            name: "Email Marketing",
110                            employees: [
111                                { name: "Vera", role: "Email Marketing Coordinator" },
112                                { name: "Lara", role: "Software Engineer" }
113                            ],
114                            subdepartments: []
115                        }
116                    ]
117                }
118            ]
119        }
120    ]
121};
122
123
124// use the filter method to find all employees in the company object with a specific role, such as "Software Engineer"
125
126function getAllSoftwareEngineerRoles(departments){
127    return departments.map(department => {
128        let names = department.employees.filter((employee) => employee.role === "Software Engineer").map(employee => employee.name);
129        if (department.subdepartments && department.subdepartments.length > 0) {
130            department.subdepartments.forEach(subdepartment => {
131                names = names.concat(getAllSoftwareEngineerRoles([subdepartment]));
132            })
133        }
134        return names;
135    }).flat()
136}
137
138const employeeNamesSoftwareEngineer = getAllSoftwareEngineerRoles(company.departments);
139
140console.log("employeeNamesSoftwareEngineer===>",employeeNamesSoftwareEngineer);
141
142
143//Add a salary property to each employee
144
145function addEmployeeSalary(departments){
146    departments.forEach(department => {
147        department.employees.forEach(employee => {
148            employee.salary = ((Math.random() * (90000 - 50000 + 1)) + 50000).toFixed(2);
149        })
150        if (department.subdepartments) {
151            addEmployeeSalary(department.subdepartments);
152        }
153    }
154    
155)}
156
157addEmployeeSalary(company.departments);
158console.log("company with salary property===>", JSON.stringify(company));
159
160