- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 have to write a function that takes in such an array and returns the average of all the ages present in the array.
Therefore, let’s write the code for this function −
Example
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));
Output
The output in the console will be −
21.8
- Related Articles
- Map multiple properties in array of objects to the same array JavaScript
- How to access properties of an array of objects in JavaScript?
- Sort Array of objects by two properties in JavaScript
- Golang Program to Calculate the Average of Numbers in a Given List
- Sort an array of objects by multiple properties in JavaScript
- Finding average age from array of Objects using JavaScript
- How to calculate the ln of a given number in JavaScript?
- Filter array of objects whose properties contains a value in JavaScript
- How to merge properties of two JavaScript Objects dynamically?
- How can we make an Array of Objects from n properties of n arrays in JavaScript?
- Explain the Properties of 2D objects in JavaFX
- Array of objects to array of arrays in JavaScript
- Calculate average of ratings in array and then include the field to original document in MongoDB?
- How to sort an array of objects based on the length of a nested array in JavaScript
- Java program to calculate the average of numbers in Java

Advertisements