Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 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:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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:

Approach 1:

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
   }
]



My Personal Notes arrow_drop_up
Recommended Articles
Page :