reduce to find a max from array javascript code example
Example 1: How to find the max id in an array of objects in JavaScript
const shots = [
{id: 1, amount: 2},
{id: 2, amount: 4},
{id: 3, amount: 52},
{id: 4, amount: 36},
{id: 5, amount: 13},
{id: 6, amount: 33}
];
shots.reduce((acc, shot) => acc = acc > shot.amount ? acc : shot.amount, 0);
Example 2: javascript hwo to return largest value with index
var a = [0, 21, 22, 7];
var indexOfMaxValue = a.reduce((iMax, x, i, arr) => x > arr[iMax] ? i : iMax, 0);
document.write("indexOfMaxValue = " + indexOfMaxValue); // prints "indexOfMaxValue = 2"