Is there an array method (or any other way really) to group objects within an array by a property and sum by another. Then return the max?
let array = [
{ticker:'AAPL',val:400},
{ticker:'IBM',val:200},
{ticker:'AAPL',val:500},
{ticker:'SBUX',val:800},
]
let obj = {}
array.forEach( h => {
obj[h.ticker] = obj[h.ticker] || 0;
obj[h.ticker] += h.val
})
let temp = Object.keys( obj ).map(function ( key ) { return obj[key] });
console.log(Math.max(...temp)) // returns 900
If you just want to modify it to use reduce instead of forEach, you can do:
let obj = array.reduce((a,b) => ({...a, [b.ticker]: (a[b.ticker] || 0) + b.val}), {});