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
How to count number of occurrences of repeated names in an array - JavaScript?
In JavaScript, counting occurrences of repeated names in an array is a common task when working with data analysis and statistics. This involves iterating through an array of objects and tracking how many times each name appears.
Let's consider an array consisting of student details with repeated names:
var details = [
{
studentName: "John",
studentAge: 23
},
{
studentName: "David",
studentAge: 24
},
{
studentName: "David",
studentAge: 20
}
];
Our goal is to count the occurrences and get results like:
John: 1 David: 2
Using reduce() Method
The reduce() method executes a reducer function on each element of the array and returns a single accumulated result. It's perfect for counting occurrences.
Syntax
array.reduce(function(accumulator, currentValue, currentIndex, arr), initialValue)
Example: Counting Numbers
<!DOCTYPE html>
<html>
<body>
<script>
var array = [10, 22, 10, 25, 10, 67, 10, 68];
var search = 10;
var count = array.reduce(function(n, val) {
return n + (val === search);
}, 0);
document.write("Occurrences of " + search + ": " + count);
</script>
</body>
</html>
Occurrences of 10: 4
Example: Counting Names in Objects
<!DOCTYPE html>
<html>
<body>
<script>
var details = [{
studentName: "John",
studentAge: 23
}, {
studentName: "David",
studentAge: 24
}, {
studentName: "John",
studentAge: 21
}, {
studentName: "Bob",
studentAge: 22
}, {
studentName: "David",
studentAge: 20
}];
var nameCounts = details.reduce((obj, {studentName}) => {
obj[studentName] = (obj[studentName] || 0) + 1;
return obj;
}, {});
document.write("Name counts: " + JSON.stringify(nameCounts, null, 2));
</script>
</body>
</html>
Name counts: {
"John": 2,
"David": 2,
"Bob": 1
}
Using for...of Loop
A simple loop approach that iterates through the array and maintains a count object:
<!DOCTYPE html>
<html>
<body>
<script>
const names = ['John', 'David', 'John', 'David', 'Bob'];
const count = {};
for (const name of names) {
count[name] = (count[name] || 0) + 1;
}
document.write("Name occurrences: " + JSON.stringify(count, null, 2));
</script>
</body>
</html>
Name occurrences: {
"John": 2,
"David": 2,
"Bob": 1
}
Using forEach() Method
The forEach() method provides another clean approach to iterate and count occurrences:
<!DOCTYPE html>
<html>
<body>
<script>
var cars = [{
"car": "BMW",
"color": "black"
}, {
"car": "Toyota",
"color": "grey"
}, {
"car": "BMW",
"color": "red"
}];
let carCounts = {};
cars.forEach(item => {
carCounts[item.car] = (carCounts[item.car] || 0) + 1;
});
document.write("Car counts: " + JSON.stringify(carCounts, null, 2));
</script>
</body>
</html>
Car counts: {
"BMW": 2,
"Toyota": 1
}
Comparison of Methods
| Method | Performance | Readability | Best For |
|---|---|---|---|
reduce() |
Good | High | Functional programming style |
for...of |
Excellent | High | Simple arrays |
forEach() |
Good | High | Object arrays |
Conclusion
All three methods effectively count name occurrences in arrays. Use reduce() for functional programming, for...of for performance, and forEach() for object arrays. Choose based on your coding style and specific requirements.
