Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find the number of times a value of an object property occurs in an array with JavaScript?
When working with arrays of objects, you often need to count how many times a specific property value appears. JavaScript's reduce() method combined with Map provides an efficient solution for this task.
Using reduce() with Map
The reduce() method processes each array element to build a frequency count. We use a Map to store the counts because it maintains insertion order and provides efficient lookups.
const subjectDetails = [
{
subjectId: '101',
subjectName: 'JavaScript'
},
{
subjectId: '102',
subjectName: 'Java'
},
{
subjectId: '103',
subjectName: 'JavaScript'
},
{
subjectId: '104',
subjectName: 'Python'
},
{
subjectId: '105',
subjectName: 'JavaScript'
}
];
const frequencyCount = [...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()];
console.log(frequencyCount);
[
{ subjectName: 'JavaScript', frequency: 3 },
{ subjectName: 'Java', frequency: 1 },
{ subjectName: 'Python', frequency: 1 }
]
How It Works
The algorithm follows these steps:
-
reduce()iterates through each object in the array - For each object, it checks if the property value already exists in the Map
- If it exists, increment the frequency counter
- If it doesn't exist, create a new entry with frequency 1
- Convert Map values back to an array using spread operator
Alternative: Using forEach with Object
You can also use a plain object instead of Map for simpler syntax:
const subjectDetails = [
{ subjectId: '101', subjectName: 'JavaScript' },
{ subjectId: '102', subjectName: 'Java' },
{ subjectId: '103', subjectName: 'JavaScript' },
{ subjectId: '104', subjectName: 'Python' }
];
const frequencyObj = {};
subjectDetails.forEach(subject => {
if (frequencyObj[subject.subjectName]) {
frequencyObj[subject.subjectName]++;
} else {
frequencyObj[subject.subjectName] = 1;
}
});
console.log(frequencyObj);
{ JavaScript: 2, Java: 1, Python: 1 }
Counting Specific Property Values
To count occurrences of any object property, create a reusable function:
function countPropertyValues(array, propertyName) {
return array.reduce((counts, obj) => {
const value = obj[propertyName];
counts[value] = (counts[value] || 0) + 1;
return counts;
}, {});
}
const students = [
{ name: 'Alice', grade: 'A' },
{ name: 'Bob', grade: 'B' },
{ name: 'Charlie', grade: 'A' },
{ name: 'David', grade: 'C' }
];
console.log(countPropertyValues(students, 'grade'));
{ A: 2, B: 1, C: 1 }
Conclusion
Use reduce() with Map for complex frequency counting with objects, or simple object accumulation for basic counting. Both approaches efficiently count property value occurrences in object arrays.
