1``filter``
2==========
3
4.. versionadded:: 2.10
5
6    The ``filter`` filter was added in Twig 2.10.
7
8The ``filter`` filter filters elements of a sequence or a mapping using an arrow
9function. The arrow function receives the value of the sequence or mapping:
10
11.. code-block:: twig
12
13    {% set sizes = [34, 36, 38, 40, 42] %}
14
15    {{ sizes|filter(v => v > 38)|join(', ') }}
16    {# output 40, 42 #}
17
18Combined with the ``for`` tag, it allows to filter the items to iterate over:
19
20.. code-block:: twig
21
22    {% for v in sizes|filter(v => v > 38) -%}
23        {{ v }}
24    {% endfor %}
25    {# output 40 42 #}
26
27It also works with mappings:
28
29.. code-block:: twig
30
31    {% set sizes = {
32        xs: 34,
33        s:  36,
34        m:  38,
35        l:  40,
36        xl: 42,
37    } %}
38
39    {% for k, v in sizes|filter(v => v > 38) -%}
40        {{ k }} = {{ v }}
41    {% endfor %}
42    {# output l = 40 xl = 42 #}
43
44The arrow function also receives the key as a second argument:
45
46.. code-block:: twig
47
48    {% for k, v in sizes|filter((v, k) => v > 38 and k != "xl") -%}
49        {{ k }} = {{ v }}
50    {% endfor %}
51    {# output l = 40 #}
52
53Note that the arrow function has access to the current context.
54
55Arguments
56---------
57
58* ``array``: The sequence or mapping
59* ``arrow``: The arrow function
60