Skip to content

map

The 'map' filter is designed to transform an array by applying a specific operation to each of its elements. It essentially creates a new array where each element is the result of the specified operation on the corresponding element in the original array.

Functionality

  • Arrays: Takes an array as input.
  • Member: Requires a string argument specifying the member or property to extract or evaluate from each element.
  • Output: Returns a new array containing the results of the operation applied to each element.

Syntax

    {{ array_variable | map: member }}

Arguments

  • member: A string representing the name of the member or property to access or evaluate on each element of the array.

Code Samples

Example 1: Extracting a Property from Objects

    {% assign products = [{ "name": "Product A", "price": 10 }, 
                          { "name": "Product B", "price": 25 }, 
                          { "name": "Product C", "price": 15 }] %}

    {{ products | map: "price" }}
Output:
[10, 25, 15]
Example 2: Evaluating a Method on Objects

    {% assign numbers = [1, 2, 3, 4] %}

    {{ numbers | map: "times2" }} 

Assuming a 'times2' method is available on the numbers:

Output:

[2, 4, 6, 8]

Outliers and Special Cases

  • Non-Array Input: If the input is not an array, the map filter returns the original input value unchanged.
  • Empty Arrays: If the input array is empty, the map filter returns an empty array.
  • Invalid Member: If the specified member does not exist on the elements of the array, the filter may return an error or unexpected results (depending on Experience Builder's error handling).

Key Points

  • The map filter is powerful for transforming and extracting data from complex array structures.
  • It allows you to perform operations on each element concisely without explicit iteration in your templates.
  • Ensure that the specified member is valid for the elements in the array to avoid errors.