filter

The filter filter filters elements of a sequence or a mapping using an arrow function. The arrow function receives the value of the sequence or mapping:

フィルター フィルターは、矢印関数を使用してシーケンスまたはマッピングの要素をフィルター処理します。アロー関数は、シーケンスまたはマッピングの値を受け取ります。
1
2
3
4
{% set sizes = [34, 36, 38, 40, 42] %}

{{ sizes|filter(v => v > 38)|join(', ') }}
{# output 40, 42 #}

Combined with the for tag, it allows to filter the items to iterate over:

for タグと組み合わせると、アイテムをフィルター処理して反復できます。
1
2
3
4
{% for v in sizes|filter(v => v > 38) -%}
    {{ v }}
{% endfor %}
{# output 40 42 #}

It also works with mappings:

マッピングでも機能します。
1
2
3
4
5
6
7
8
9
10
11
12
{% set sizes = {
    xs: 34,
    s:  36,
    m:  38,
    l:  40,
    xl: 42,
} %}

{% for k, v in sizes|filter(v => v > 38) -%}
    {{ k }} = {{ v }}
{% endfor %}
{# output l = 40 xl = 42 #}

The arrow function also receives the key as a second argument:

アロー関数も 2 番目の引数としてキーを受け取ります。
1
2
3
4
{% for k, v in sizes|filter((v, k) => v > 38 and k != "xl") -%}
    {{ k }} = {{ v }}
{% endfor %}
{# output l = 40 #}

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

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

Arguments

  • array: The sequence or mapping
    配列: シーケンスまたはマッピング
  • arrow: The arrow function
    arrow: アロー関数