I am trying to count the occurrences of values for multiple objects in an array in Javascript. For the following example input (original data is much larger):
input= [
{
Name: "James",
Department: "Engineering",
employeeId: 26
},
{
Name: "Roger",
Department: "Design",
employeeId: 21
},
{
Name: "John",
Department: "Engineering",
employeeId: 32
},
{
Name: "Roger",
Department: "Engineering",
employeeId: 42
},
{
Name: "Roger",
Department: "Engineering",
employeeId: 36
}
For this I am trying to get the following output:
Output = [
{Name: 'James', Department: 'Engineering', count:1},
{Name: 'Roger', Department: 'Design', count:1},
{Name: 'John', Department: 'Engineering', count:1},
{Name: 'Roger', Department: 'Engineering', count:2},
]
I am trying to use loops and if statements given that I am new to JS. I tried implementing variations to what is provided on
While its counting correctly, I cant seem to capture all the groupings. Any help here would be really appreciated!
Thanks!