I have an array of nested objects:
const members = [
{
name: 'Mike',
type: 'MANAGER',
children: []
},
{
name: 'John',
type: 'MANAGER',
children: [
{
name: 'George',
type: 'EMPLOYEE',
children: []
},
{
name: 'Jake',
type: 'EMPLOYEE',
children: []
},
{
name: 'Fred',
type: 'MANAGER',
children: [
{
name: 'Henry',
type: 'EMPLOYEE',
children: []
},
{
name: 'Julie',
type: 'MANAGER',
children: []
}
]
}
]
},
{
name: 'Alex',
type: 'MANAGER',
children: [
{
name: 'Mark',
type: 'EMPLOYEE',
children: []
},
{
name: 'Ashley',
type: 'MANAGER',
children: []
}
]
}
];
How can I recursively remove all of the EMPLOYEE
objects from the array and add them to a new array?
I’m able to add all of the EMPLOYEE
members to a new array, but I’m not sure how to remove them from the original array.
const employees = [];
const extractEmployees = (members) => {
for(let i = 0; i < members.length; i++) {
const member = members[i];
// add employee to employees array
if(member.type === 'EMPLOYEE') {
employees.push(member);
}
if(member.children) {
// call function recursively for member children
extractEmployees(member.children);
}
}
}
// initialize
extractEmployees(members);
2
Answers
The
extractEmployees
can return a new array of members (newMembers
) that doesn’t include the employees. This array is assigned to themember.children
:The previous solution replaces the original
children
arrays. If you want to keep the original arrays, you can save the items in another array, empty the originalmembers
and add to it non-employees:It is probably better practice to not collect the employees in a global variable. One way to collect them is to yield them from a generator.
The example in the question had no employees that had "children". If that is a possible scenario, then you might want to collect those employee records without those children, as those could also have employees which would be collected separately, leading to some duplication.
Finally, to filter inplace, you could use the two-index approach where items are packed at the left side of the array (when they should not be removed) and then the array length can be adapted accordingly.
Here is code that implements those ideas:
If you really want to collect the unaltered employees (even with children), then do
yield member
instead ofyield rest
.