blob: 1083bfe5eae7e19261227000036486b87bbffa6d [file] [log] [blame]
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001``map``
2=======
3
4The ``map`` filter applies an arrow function to the elements of a sequence or a
5mapping. The arrow function receives the value of the sequence or mapping:
6
7.. code-block:: twig
8
9 {% set people = [
10 {first: "Bob", last: "Smith"},
11 {first: "Alice", last: "Dupond"},
12 ] %}
13
14 {{ people|map(p => "#{p.first} #{p.last}")|join(', ') }}
15 {# outputs Bob Smith, Alice Dupond #}
16
17The arrow function also receives the key as a second argument:
18
19.. code-block:: twig
20
21 {% set people = {
22 "Bob": "Smith",
23 "Alice": "Dupond",
24 } %}
25
26 {{ people|map((last, first) => "#{first} #{last}")|join(', ') }}
27 {# outputs Bob Smith, Alice Dupond #}
28
29Note that the arrow function has access to the current context.
30
31Arguments
32---------
33
34* ``arrow``: The arrow function