I have the following array of objects
const users = [
{ name: 'Bob Johnson', occupation: 'Developer' },
{ name: 'Joe Davidson', occupation: 'QA Tester' },
{ name: 'Kat Matthews', occupation: 'Developer' },
{ name: 'Ash Lawrence', occupation: 'Developer' },
{ name: 'Jim Powers', occupation: 'Project Manager}]
I want to create an object that stores the unique counts of the different types of occupations, like so
{ developerCount: 3, qaTesterCount: 1, projectManagerCount: 1 }
My current solution is this long and drawn out method
let occupationCounts = {
developerCount: 0,
qaTesterCount: 0,
projectManagerCount: 0
}
// Loop through the array and count the properties
users.forEach((user) => {
switch(user.occupation){
case 'Developer':
occupationCounts.developerCount++;
break;
// and so on for each occupation
}
});
This method will only grow for every new type of occupation I have to add.
Is there a simpler solution to this? (Pure javascript or Lodash Wizardry)
Any input is appreciated!