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
Counting duplicates and aggregating array of objects in JavaScript
Counting duplicates and aggregating arrays of objects is a common task in JavaScript data processing. This involves identifying duplicate elements and creating new structures that group and count related data.
Understanding the Problem
The goal is to transform an array of objects by grouping similar items and counting occurrences. For example, if we have users with different skills, we want to group by skill and collect all users for each skill.
What is Aggregating Array of Objects?
Aggregation combines multiple objects into a summarized structure. Instead of having duplicate entries, we group related data together with counts or collections of related values.
Input Example:
const details = [
{ skill: 'Python', user: 'Preet' },
{ skill: 'Javascript', user: 'Shreya' },
{ skill: 'Javascript', user: 'Kajal' },
{ skill: 'Java', user: 'Preet' },
{ skill: 'Javascript', user: 'Preet' },
{ skill: 'HTML and CSS', user: 'Happy' }
];
Desired Output:
[
{
"skill": "Python",
"users": ["Preet"]
},
{
"skill": "Javascript",
"users": ["Shreya", "Kajal", "Preet"]
},
{
"skill": "Java",
"users": ["Preet"]
},
{
"skill": "HTML and CSS",
"users": ["Happy"]
}
]
Method 1: Using reduce() for Grouping
const details = [
{ skill: 'Python', user: 'Preet' },
{ skill: 'Javascript', user: 'Shreya' },
{ skill: 'Javascript', user: 'Kajal' },
{ skill: 'Java', user: 'Preet' },
{ skill: 'Javascript', user: 'Preet' }
];
function groupBySkill(data) {
const grouped = data.reduce((acc, item) => {
if (!acc[item.skill]) {
acc[item.skill] = [];
}
acc[item.skill].push(item.user);
return acc;
}, {});
return Object.entries(grouped).map(([skill, users]) => ({
skill,
users,
count: users.length
}));
}
const result = groupBySkill(details);
console.log(result);
[
{ skill: 'Python', users: ['Preet'], count: 1 },
{ skill: 'Javascript', users: ['Shreya', 'Kajal', 'Preet'], count: 3 },
{ skill: 'Java', users: ['Preet'], count: 1 }
]
Method 2: Using Map for Better Performance
function countAndGroup(data, groupBy, collectField) {
const map = new Map();
data.forEach(item => {
const key = item[groupBy];
if (map.has(key)) {
map.get(key).push(item[collectField]);
} else {
map.set(key, [item[collectField]]);
}
});
return Array.from(map, ([key, values]) => ({
[groupBy]: key,
[collectField + 's']: values,
count: values.length
}));
}
const data = [
{ category: 'fruit', name: 'Apple' },
{ category: 'fruit', name: 'Orange' },
{ category: 'vegetable', name: 'Carrot' },
{ category: 'fruit', name: 'Banana' }
];
const grouped = countAndGroup(data, 'category', 'name');
console.log(grouped);
[
{ category: 'fruit', names: ['Apple', 'Orange', 'Banana'], count: 3 },
{ category: 'vegetable', names: ['Carrot'], count: 1 }
]
Comparison
| Method | Time Complexity | Space Complexity | Readability |
|---|---|---|---|
| reduce() | O(n) | O(n) | High |
| Map | O(n) | O(n) | Medium |
| Nested loops | O(n²) | O(n) | Low |
Real-world Example
// Sales data aggregation
const sales = [
{ product: 'Laptop', salesperson: 'Alice', amount: 1200 },
{ product: 'Phone', salesperson: 'Bob', amount: 800 },
{ product: 'Laptop', salesperson: 'Charlie', amount: 1100 },
{ product: 'Phone', salesperson: 'Alice', amount: 750 }
];
function aggregateSales(data) {
const result = data.reduce((acc, sale) => {
if (!acc[sale.product]) {
acc[sale.product] = {
product: sale.product,
totalSales: 0,
salespeople: [],
count: 0
};
}
acc[sale.product].totalSales += sale.amount;
acc[sale.product].salespeople.push(sale.salesperson);
acc[sale.product].count++;
return acc;
}, {});
return Object.values(result);
}
console.log(aggregateSales(sales));
[
{
product: 'Laptop',
totalSales: 2300,
salespeople: ['Alice', 'Charlie'],
count: 2
},
{
product: 'Phone',
totalSales: 1550,
salespeople: ['Bob', 'Alice'],
count: 2
}
]
Conclusion
The reduce() method provides the most efficient and readable solution for counting duplicates and aggregating objects. It offers O(n) time complexity and handles complex grouping scenarios effectively.
