UNPKG

locutus

Version:

Locutus other languages' standard libraries to JavaScript for fun and educational purposes

13 lines (12 loc) 532 B
export function array_reduce(aInput, callback, initial) { // discuss at: https://locutus.io/php/array_reduce/ // original by: Alfonso Jimenez (https://www.alfonsojimenez.com) // note 1: Takes a function as an argument, not a function's name // example 1: array_reduce([1, 2, 3, 4, 5], function (v, w){v += w;return v;}) // returns 1: 15 let carry = typeof initial === 'undefined' ? null : initial; for (const value of aInput) { carry = callback(carry, value); } return carry; }