Prism · Arrays
$reduce
Folds an array into one value. The element and what has been gathered so far are both in scope inside body; the accumulator is called acc unless named.
{ numbers: [3, 1, 4, 1, 5, 9, 2, 6] }{
$reduce: {
over: { $ref: '$.numbers' },
as: 'n',
acc: 'total',
init: { $const: 0 },
body: { $add: [{ $var: 'total' }, { $var: 'n' }] },
},
}31
Runs in this page on the installed package. The input and the expected output are defined by the example.