How to add all the values in a array of objects code example
Example 1: sum of array of objects javascript
var accounts = [
{ name: 'James Brown', msgCount: 123 },
{ name: 'Stevie Wonder', msgCount: 22 },
{ name: 'Sly Stone', msgCount: 16 },
{ name: 'Otis Redding', msgCount: 300 }
];
var msgTotal = accounts.reduce(function(prev, cur) {
return prev + cur.msgCount;
}, 0);
console.log('Total Messages:', msgTotal);
Example 2: javascript object array sum of values in object
const fruitTally = fruit.reduce((currentTally, currentFruit) => {
currentTally[currentFruit] = (currentTally[currentFruit] || 0) + 1
return currentTally
} , {})