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