Edit on GitHub
Jump to docs navigation

Twig Components / Methods / reduce

Note: You are currently reading the documentation for Bolt 5.0. Looking for the documentation for Bolt 5.2 instead?

reduce(arrow, initial = null) is a Twig filter to iteratively reduce a sequence or a mapping to a single value using an arrow function, so as to reduce it to a single value. The arrow function receives the return value of the previous iteration and the current value of the sequence or mapping.

{% set numbers = [1, 2, 3] %}

{{ numbers|reduce((carry, v) => carry + v) }}
{# output 6 #}

The reduce filter takes an initial value as a second argument:

{{ numbers|reduce((carry, v) => carry + v, 10) }}
{# output 16 #}

Note: the arrow function has access to the current context.

ArgumentsΒΆ

  • arrow: The arrow function
  • initial: The initial value

Source: Twig



Edit this page on GitHub
Couldn't find what you were looking for? We are happy to help you in the forum, on Slack or on Github.