- 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
Find the number of times a value of an object property occurs in an array with JavaScript?
For this, use the concept of reduce(). Following is the code −
Example
const subjectDetails = [ { subjectId: '101', subjectName: 'JavaScript' }, { subjectId: '102', subjectName: 'Java' }, { subjectId: '103', subjectName: 'JavaScript' } ]; console.log([...subjectDetails.reduce((obj1, obj2) => { if (obj1.has(obj2.subjectName)){ obj1.get(obj2.subjectName).frequency++; } else { obj1.set(obj2.subjectName, { subjectName: obj2.subjectName, frequency: 1 }); } return obj1; }, new Map()).values()]);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo144.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo144.js [ { subjectName: 'JavaScript', frequency: 2 }, { subjectName: 'Java', frequency: 1 } ]
- Related Articles
- Increment a property value of an element in array object with MongoDB
- JavaScript Count the number of unique elements in an array of objects by an object property?
- How to count the number of times a value occurs in a column of an R data frame?
- Sorting an array object by property having falsy value - JavaScript
- Take an array and find the one element that appears an odd number of times in JavaScript
- Find the closest value of an array in JavaScript
- Increment value of an array element with array object in MongoDB
- How to modify an array value of a JSON object in JavaScript?
- How to find the one integer that appears an odd number of times in a JavaScript array?
- How to return an array whose elements are the enumerable property values of an object in JavaScript?
- How to find inside an array of objects the object that holds the highest value in JavaScript?
- How to find the maximum value of an array in JavaScript?
- How to find the minimum value of an array in JavaScript?
- How to delete a property of an object in JavaScript?
- How to create an object property from a variable value in JavaScript?

Advertisements