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
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" }}
Assuming a 'times2' method is available on the numbers:
Output:
Outliers and Special Cases¶
- Non-Array Input: If the input is not an array, the
mapfilter returns the original input value unchanged. - Empty Arrays: If the input array is empty, the
mapfilter returns an empty array. - Invalid Member: If the specified
memberdoes 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
mapfilter 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
memberis valid for the elements in the array to avoid errors.