JavaScript - Count Number of Occurrences of Repeated Names in an Array of Objects
Here are the various methods to count the number of occurrences of repeated names in an array of objects in JavaScript
1. Using a Basic Loop with an Object
This is the simplest approach that uses a for loop and an object to store the counts.
//Driver Code Starts
const a = [
{ name: "Alia" }, { name: "Bharat" }, { name: "Shriya" }, { name: "Charu" },
{ name: "Tanu" }, { name: "Tanya" },
];
//Driver Code Ends
const Loop = (arr) => {
const c = {};
for (const { name } of arr) {
c[name] = (c[name] || 0) + 1;
}
return c;
};
//Driver Code Starts
const res = Loop(a);
console.log(res);
//Driver Code Ends
Output
{ Alia: 1, Bharat: 1, Shriya: 1, Charu: 1, Tanu: 1, Tanya: 1 }
In this example:
- The function iterates over the array, extracting the name property from each object.
- It initializes or increments the count for each name in the counts object.
2. Using Map
The code counts repeated names in an array of objects by extracting names, identifying unique ones, and mapping them with their respective counts into a new array of objects.
//Driver Code Starts
const a = [{ name: "Riya" }, { name: "Nisha" }, { name: "Tanisha" }, { name: "Jaya" },
{ name: "Bobby" }, { name: "Alia" },
];
//Driver Code Ends
const map = (arr) => {
const nameMap = new Map();
arr.forEach(({ name }) => {
nameMap.set(name, (nameMap.get(name) || 0) + 1);
});
return Object.fromEntries(nameMap);
};
//Driver Code Starts
const res = map(a);
console.log(res);
//Driver Code Ends
Output
{ Riya: 1, Nisha: 1, Tanisha: 1, Jaya: 1, Bobby: 1, Alia: 1 }
In this example:
- The function map counts occurrences of names in an array of objects.
- It uses a Map to store names as keys and their counts as values, updating the count for each name during iteration.
- Finally, it converts the Map to an object and returns the result.
3. Using Array.prototype.reduce
The reduce
method allows a more functional programming approach.
//Driver Code Starts
const a = [{ name: "Riya" }, { name: "Nisha" }, { name: "Tanisha" }, { name: "Jaya" },
{ name: "Bobby" }, { name: "Alia" },
];
//Driver Code Ends
const reduce = (arr) => {
return arr.reduce((acc, { name }) => {
acc[name] = (acc[name] || 0) + 1;
return acc;
}, {});
};
//Driver Code Starts
const res = reduce(a);
console.log(res);
//Driver Code Ends
Output
{ Riya: 1, Nisha: 1, Tanisha: 1, Jaya: 1, Bobby: 1, Alia: 1 }
In this example:
- The reduce function iterates through the array, updating an accumulator object with the count of each name.
- This method is concise and expressive.
4. Using Array.prototype.filter
This approach uses filter to count occurrences for each unique name. While less efficient, it’s simpler for small datasets.
//Driver Code Starts
const a = [{ name: "Riya" }, { name: "Nisha" }, { name: "Tanisha" }, { name: "Jaya" },
{ name: "Bobby" }, { name: "Alia" },
];
//Driver Code Ends
const filter = (arr) => {
const b = [...new Set(arr.map(({ name }) => name))];
return b.reduce((acc, Name) => {
acc[b] = arr.filter(({ b }) => b === Name).length;
return acc;
}, {});
};
//Driver Code Starts
const res = filter(a);
console.log(res);
//Driver Code Ends
Output
{ 'Riya,Nisha,Tanisha,Jaya,Bobby,Alia': 0 }
In this example:
- Extract unique names using Set.
- Use filter to count occurrences for each unique name.
- Combine results into an object.
5. Using External Libraries
For complex projects, using libraries like Lodash can simplify such operations.
const _ = require("lodash");
const a = [{ name: "Riya" }, { name: "Nisha" }, { name: "Tanisha" }, { name: "Jaya" },
{ name: "Bobby" }, { name: "Alia" },
];
const res = _.countBy(a, "name");
console.log(res);
In this example:
- The _.countBy method groups and counts occurrences of names directly.
- This approach is concise and reliable for larger codebases.
Choosing the Right Approach
Approach | Suitable For | Complexity |
---|---|---|
Basic Loop | Simplicity, small datasets | O(n) |
Functional programming | O(n) | |
Efficiency, large datasets | O(n) | |
Simplicity, small datasets | O(n^2) | |
External Libraries | Large projects | O(n) |