Paul has a point. When you start to write code like that, it is time to question whether there is a better alternative.
How many departments do pass the test? Would that be easier to test, rather than exclusions?
There maybe more perfomant solutions, but I have opted to put the exclusions into an array. That way Array.includes can be used to test against all these exclusions.
const exclusions = [
'Academic Affairs',
'Student Affairs',
'Communications & Marketing',
'Enrollment Management',
'Enterprise Information Technology',
'IRPA',
'Learning and Technology',
'Human & Environmental Services',
'Institutional Advancement',
'Finance'
]
Example:
exclusions.includes('IRPA') // true
exclusions.includes('Commerce') // false
Filter departments function
Then as Paul suggested put the departments test into a function.
const filterDepartments = (faculty) => {
return faculty.flatMap((user) => {
const department = user.department
// if department exists
if (department) {
// If the department is in the excluded list return an empty array.
// This will be filtered out, otherwise return the user
return (exclusions.includes(department)) ? [] : user
}
// There is no department, return an empty array.
// Again this will be filtered out.
return []
})
}
A quick test
const faculties = [
{ id: 3, department: 'Academic Affairs' },
{ id: 2, department: 'Economics' },
{ id: 3, department: 'IRPA' },
{ id: 4, department: 'Commerce' },
{ id: 5, department: '' },
{ id: 6 }
]
console.log(filterDepartments(faculties))
/*
[
{id: 2, department: 'Economics'},
{id: 4, department: 'Commerce'}
]
*/
Edit:
Just an another thought. You may consider changing those exclusions to all lowercase.
Then testing like this
return (exclusions.includes(department.toLowerCase())) ? [] : user
That way it would still pickup on matches with slight typos e.g. ‘Enrollment management’