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
Aggregate records in JavaScript
Aggregating records in JavaScript involves combining multiple objects with the same identifier into a single object with accumulated values. This is commonly used for data analysis tasks like summing transactions by person or grouping sales by product.
Let's say we have an array of objects that contains information about some random transactions carried out by some people:
const transactions = [{
name: 'Rakesh',
amount: 1500
}, {
name: 'Rajesh',
amount: 1200
}, {
name: 'Ramesh',
amount: 1750
}, {
name: 'Rakesh',
amount: 2100
}, {
name: 'Mukesh',
amount: 1100
}, {
name: 'Rajesh',
amount: 1950
}, {
name: 'Mukesh',
amount: 1235
}, {
name: 'Ramesh',
amount: 2000
}];
console.log("Original transactions:", transactions);
Original transactions: [
{ name: 'Rakesh', amount: 1500 },
{ name: 'Rajesh', amount: 1200 },
{ name: 'Ramesh', amount: 1750 },
{ name: 'Rakesh', amount: 2100 },
{ name: 'Mukesh', amount: 1100 },
{ name: 'Rajesh', amount: 1950 },
{ name: 'Mukesh', amount: 1235 },
{ name: 'Ramesh', amount: 2000 }
]
We need to write a function that takes this array and aggregates the transaction amounts of unique people into distinct objects.
Using Array.reduce() Method
The most efficient approach uses the reduce() method to accumulate values:
const aggregateArray = arr => {
return arr.reduce((acc, val) => {
const index = acc.findIndex(obj => obj.name === val.name);
if(index !== -1){
acc[index].amount += val.amount;
}else{
acc.push({
name: val.name,
amount: val.amount
});
};
return acc;
}, []);
};
console.log(aggregateArray(transactions));
[
{ name: 'Rakesh', amount: 3600 },
{ name: 'Rajesh', amount: 3150 },
{ name: 'Ramesh', amount: 3750 },
{ name: 'Mukesh', amount: 2335 }
]
Using Map for Better Performance
For larger datasets, using a Map provides better performance than findIndex():
const aggregateWithMap = arr => {
const map = new Map();
arr.forEach(transaction => {
if (map.has(transaction.name)) {
map.set(transaction.name, map.get(transaction.name) + transaction.amount);
} else {
map.set(transaction.name, transaction.amount);
}
});
return Array.from(map.entries()).map(([name, amount]) => ({ name, amount }));
};
console.log(aggregateWithMap(transactions));
[
{ name: 'Rakesh', amount: 3600 },
{ name: 'Rajesh', amount: 3150 },
{ name: 'Ramesh', amount: 3750 },
{ name: 'Mukesh', amount: 2335 }
]
Using Object for Grouping
Another approach uses a plain object to group and sum values:
const aggregateWithObject = arr => {
const grouped = arr.reduce((acc, transaction) => {
acc[transaction.name] = (acc[transaction.name] || 0) + transaction.amount;
return acc;
}, {});
return Object.entries(grouped).map(([name, amount]) => ({ name, amount }));
};
console.log(aggregateWithObject(transactions));
[
{ name: 'Rakesh', amount: 3600 },
{ name: 'Rajesh', amount: 3150 },
{ name: 'Ramesh', amount: 3750 },
{ name: 'Mukesh', amount: 2335 }
]
Comparison of Methods
| Method | Performance | Readability | Memory Usage |
|---|---|---|---|
| Array.reduce() with findIndex() | O(n²) - slower | Good | Low |
| Map | O(n) - fastest | Good | Medium |
| Object grouping | O(n) - fast | Best | Low |
Conclusion
For aggregating records in JavaScript, use the Object grouping method for simplicity and performance. The Map approach is ideal for complex keys, while Array.reduce() works well for smaller datasets.
