Skip to content

Instantly share code, notes, and snippets.

@mojaray2k
Last active August 23, 2018 17:47
Show Gist options
  • Save mojaray2k/057bcde7b8e4d889fdc500826d334afb to your computer and use it in GitHub Desktop.
Save mojaray2k/057bcde7b8e4d889fdc500826d334afb to your computer and use it in GitHub Desktop.
The Reduce Array Method Example 4
/**
* Distance Traveled
* Use the 'reduce' helper to find the sum of all the distances traveled.
* Assign the result to the variable 'totalDistance'
*/
const trips = [{ distance: 34 }, { distance: 12 } , { distance: 1 }];
const totalDistance = trips.reduce((total, trip) => {
total += trip.distance
return total;
}, 0);
console.log(totalDistance);
/ Result is: 47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment