
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Occurrences of Object Property Value in 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 } ]
Advertisements