How to count number of occurrences of repeated names in an array of objects in JavaScript ? - GeeksforGeeks

24 Apr, 2021
Given an array of objects and the task is to find the occurrences of a given key according to its value.
Example:
Input : arr = [
   {
      employeeName: "Ram",
      employeeId: 23
   },
   {
      employeeName: "Shyam",
      employeeId: 24
   },
   {
      employeeName: "Ram",
      employeeId: 21
   },
   {
      employeeName: "Ram",
      employeeId: 25
   },
   {
      employeeName: "Kisan",
      employeeId: 22
   },
   {
      employeeName: "Shyam",
      employeeId: 20
   }
]

key = "employeeName"

Output: [
    {employeeName: "Ram", occurrences: 3},
    {employeeName: "Shyam", occurrences: 2},
    {employeeName: "Kisan", occurrences:  1}
] 
To achieve this we have the following approaches:
In this approach, we follow the steps below.
Create an empty output array.
Using the forEach iterate the input array.
Check if the output array contains any object which contains the provided key’s value
If not, then create a new object and initialize the object with the key(the provided key name) set to value (the key’s value of the object of the present iteration) and occurrence set to value 1
If yes, then iterate the output array and search if the key of the present iteration is equal to the key of the input array iteration then increase the occurrence to 1.
Javascript
<script>function findOcc(arr, key){  let arr2 = [];      arr.forEach((x)=>{           // Checking if there is any object in arr2    // which contains the key value     if(arr2.some((val)=>{ return val[key] == x[key] })){                // If yes! then increase the occurrence by 1       arr2.forEach((k)=>{         if(k[key] === x[key]){            k["occurrence"]++         }      })              }else{       // If not! Then create a new object initialize        // it with the present iteration key's value and        // set the occurrence to 1       let a = {}       a[key] = x[key]       a["occurrence"] = 1       arr2.push(a);     }  })      return arr2}    let arr = [   {      employeeName: "Ram",      employeeId: 23   },   {      employeeName: "Shyam",      employeeId: 24   },   {      employeeName: "Ram",      employeeId: 21   },   {      employeeName: "Ram",      employeeId: 25   },   {      employeeName: "Kisan",      employeeId: 22   },   {      employeeName: "Shyam",      employeeId: 20   }]  let key = "employeeName"console.log(findOcc(arr, key))</script>
Output:
[
   {
      employeeName: "Ram",
      occurrence: 3
   }, 
   {
      employeeName: "Shyam",
     occurrence: 2
   }, 
   {
    employeeName: "Kisan",
    occurrence: 1
   }
]
javascript-array
javascript-basics
JavaScript-Questions
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Convert a string to an integer in JavaScript
How to calculate the number of days between two dates in javascript?
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
File uploading in React.js
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?