- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to count number of occurrences of repeated names in an array - JavaScript?
Let’s say the following is our array −
var details = [ { studentName: "John", studentAge: 23 }, { studentName: "David", studentAge: 24 }, { studentName: "John", studentAge: 21 }, { studentName: "John", studentAge: 25 }, { studentName: "Bob", studentAge: 22 }, { studentName: "David", studentAge: 20 } ]
We need to count of occurrence of repeated names i.e. the output should be
John: 3 David: 2 Bob: 1
For this, you can use the concept of reduce().
Example
Following is the code −
var details = [ { studentName: "John", studentAge: 23 }, { studentName: "David", studentAge: 24 }, { studentName: "John", studentAge: 21 }, { studentName: "John", studentAge: 25 }, { studentName: "Bob", studentAge: 22 }, { studentName: "David", studentAge: 20 } ] var output = Object.values(details.reduce((obj, { studentName }) => { if (obj[studentName] === undefined) obj[studentName] = { studentName: studentName, occurrences: 1 }; else obj[studentName].occurrences++; return obj; }, {})); console.log(output);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo282.js. This will produce the following output on console −
PS C:\Users\Amit\javascript-code> node demo282.js [ { studentName: 'John', occurrences: 3 }, { studentName: 'David', occurrences: 2 }, { studentName: 'Bob', occurrences: 1 } ]
- Related Articles
- Convert array with duplicate values to object with number of repeated occurrences in JavaScript
- Unique number of occurrences of elements in an array in JavaScript
- Count occurrences of a character in a repeated string in C++
- How to count the number of occurrences of a character in a string in JavaScript?
- How to count total number of occurrences of an object in a Python list?
- Count number of occurrences (or frequency) in a sorted array in C++
- How to count the number of occurrences of all unique values in an R data frame?
- Count the number of data types in an array - JavaScript
- How to count the number of elements in an array below/above a given number (JavaScript)
- Count number of occurrences for each char in a string with JavaScript?
- Count occurrences of the average of array elements with a given number in C++
- How to count the number of repeated characters in a Golang String?
- How to validate if an element in an array is repeated? - JavaScript
- How to find the number of occurrences of unique and repeated characters in a string vector in R?
- Iterating through an array, adding occurrences of a true in JavaScript

Advertisements