
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to iterate over objects in array and sum a property in JavaScript
Suppose we have an array of objects like this −
const arr = [ { duration: 10, any: 'fields' }, { duration: 20, any: 'other fields' }, { duration: 15, any: 'some other fields' } ];
We are required to write a JavaScript function that takes in one such array and returns the summed result of the duration properties of all the objects.
For the above array, the output should be 45.
Example
The code for this will be −
const arr = [ { duration: 10, any: 'fields' }, { duration: 20, any: 'other fields' }, { duration: 15, any: 'some other fields' } ]; const addDuration = arr => { let res = 0; for(let i = 0; i < arr.length; i++){ res += arr[i].duration; }; return res; }; console.log(addDuration(arr));
Output
The output in the console −
45
- Related Articles
- How to iterate over arrays and objects in jQuery?
- Iterate over an object and remove false property in JavaScript
- How to iterate an array of objects and build a new one in JavaScript ?
- Sum of array object property values in new array of objects in JavaScript
- Add property to common items in array and array of objects - JavaScript?
- How to iterate over a Hashmap in Kotlin?
- How to iterate over a list in Java?
- C++ Program to Iterate Over an Array
- Swift Program to Iterate Over an Array
- Filter array of objects by a specific property in JavaScript?
- How to iterate json array – JavaScript?
- How to iterate over rows in a DataFrame in Pandas?
- How to iterate over a Java list?
- How to iterate over a C# dictionary?
- How to iterate over a C# list?

Advertisements