JSworkingevergreen

Array.reduce patterns

One loop, any shape of answer.

reduce folds an array into a single value of any shape — a number, an object, a map — by threading an accumulator through every element; learn the handful of patterns and you stop reaching for manual loops.

Sum and average a field

JSintro
Press Run to execute

The fold collapses four order objects into a single total: 180, then average: 45 derives from it — the length guard means an empty array yields 0 instead of NaN (dividing by zero) or a throw.