@ryanwgough
var someNumbers = [1, 2, 3, 4, 5]; var sum = someNumbers.reduce( (acc, item) => acc + item, 0 ); // sum === 15
var someColours = [ 'red', 'blue', 'green', 'red', 'blue', 'orange' ]; var count = someColours.reduce(function(acc, item) { acc[item] = acc[item] ? acc[item] + 1 : 1; return acc; }, {}); // count === { red: 2, blue: 2, green: 1, orange: 1 }
function addTwo(item) { return item + 2; } function negate(item) { return 0 - item; } var functions = [addTwo, negate]; var answer = functions.reduce(function(acc, item) { return item(acc); }, 2); // answer === -4
By Ryan Gough
Software developer, C# by day, Erlang and JS by night.