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 calculate the average in JavaScript of the given properties in the array of objects
We have an array of objects. Each object contains a few properties and one of these properties is age:
const people = [
{
name: 'Anna',
age: 22
}, {
name: 'Tom',
age: 34
}, {
name: 'John',
age: 12
}, {
name: 'Kallis',
age: 22
}, {
name: 'Josh',
age: 19
}
]
We need to write a function that takes in such an array and returns the average of all the ages present in the array.
Method 1: Using reduce()
The most efficient approach is to use the reduce() method to sum all ages and divide by the array length:
const people = [
{
name: 'Anna',
age: 22
}, {
name: 'Tom',
age: 34
}, {
name: 'John',
age: 12
}, {
name: 'Kallis',
age: 22
}, {
name: 'Josh',
age: 19
}
]
const findAverageAge = (arr) => {
const { length } = arr;
return arr.reduce((acc, val) => {
return acc + (val.age/length);
}, 0);
};
console.log(findAverageAge(people));
21.8
Method 2: Sum First, Then Divide
An alternative approach is to first calculate the total sum, then divide by the length:
const people = [
{ name: 'Anna', age: 22 },
{ name: 'Tom', age: 34 },
{ name: 'John', age: 12 },
{ name: 'Kallis', age: 22 },
{ name: 'Josh', age: 19 }
];
const findAverageAge = (arr) => {
const totalAge = arr.reduce((sum, person) => sum + person.age, 0);
return totalAge / arr.length;
};
console.log(findAverageAge(people));
21.8
Method 3: Using map() and reduce()
You can also extract ages first using map(), then calculate the average:
const people = [
{ name: 'Anna', age: 22 },
{ name: 'Tom', age: 34 },
{ name: 'John', age: 12 },
{ name: 'Kallis', age: 22 },
{ name: 'Josh', age: 19 }
];
const findAverageAge = (arr) => {
const ages = arr.map(person => person.age);
const sum = ages.reduce((acc, age) => acc + age, 0);
return sum / ages.length;
};
console.log(findAverageAge(people));
21.8
Generalized Function for Any Property
Here's a reusable function that can calculate the average of any numeric property:
const data = [
{ name: 'Anna', age: 22, salary: 50000 },
{ name: 'Tom', age: 34, salary: 75000 },
{ name: 'John', age: 12, salary: 0 }
];
const findAverage = (arr, property) => {
const sum = arr.reduce((acc, obj) => acc + obj[property], 0);
return sum / arr.length;
};
console.log("Average age:", findAverage(data, 'age'));
console.log("Average salary:", findAverage(data, 'salary'));
Average age: 22.666666666666668 Average salary: 41666.666666666664
Comparison
| Method | Performance | Readability | Reusability |
|---|---|---|---|
| reduce() with division inside | Good | Medium | Low |
| Sum first, then divide | Best | High | Medium |
| map() then reduce() | Good | High | Medium |
| Generalized function | Good | High | High |
Conclusion
The most efficient approach is to sum all values using reduce() and then divide by the array length. For reusability, create a generalized function that accepts the property name as a parameter.
