Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame] | 1 | ``map`` |
| 2 | ======= |
| 3 | |
| 4 | The ``map`` filter applies an arrow function to the elements of a sequence or a |
| 5 | mapping. 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 | |
| 17 | The 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 | |
| 29 | Note that the arrow function has access to the current context. |
| 30 | |
| 31 | Arguments |
| 32 | --------- |
| 33 | |
| 34 | * ``arrow``: The arrow function |