reduce

The reduce filter iteratively reduces 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:

reduce フィルターは、アロー関数を使用してシーケンスまたはマッピングを単一の値に繰り返し減らし、単一の値に減らします。アロー関数は、前の反復の戻り値と、シーケンスまたはマッピングの現在の値を受け取ります。
1
2
3
4
{% set numbers = [1, 2, 3] %}

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

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

reduce フィルターは、2 番目の引数として初期値を取ります。
1
2
{{ numbers|reduce((carry, v) => carry + v, 10) }}
{# output 16 #}

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

アロー関数は現在のコンテキストにアクセスできることに注意してください。

Arguments

  • arrow: The arrow function
    arrow: アロー関数
  • initial: The initial value
    initial: 初期値