JS
var arr = new Array(1,2.3,2,3,4,8,12,43,-4,-1); var func = function (accu, elem) { return Math.max(accu,elem); } var ret = arr.reduce(func); alert(ret); //43
The specified function can take 4 arguments in the order of accumulator, value, index (optional) and the array (optional).
ret = arr.reduce(func, 45); alert(ret); //45 var func2 = function (accu, elem) { return accu * elem; } ret = arr.reduce(func2); alert(ret); //911462.4 ret = arr.reduce(func2, 2); alert(ret); //1822924,8
Let's get the unique elements of an array:
var arr = new Array(4,32,29,4,2,3,4,32); var ret = arr.reduce( function(accu, elem) { if (!accu.includes(elem)) { accu.push(elem); }; return accu; },[]); alert(ret); //4,32,29,2,3