#foreach
Loops over the elements of an array or the properties of an object, transforming each with a content template.
"#foreach(@item;@index? in source) into 'name' | @variable": [ template ]Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
@item |
variable |
Bound to each element or property value. |
@index (optional) |
variable |
Bound to the zero-based index. |
source |
path | value |
The array or object to iterate. |
Returns
Section titled “Returns”array — One transformed template per element.
- Variables declared inside the template exist only for one iteration.
- When iterating an object, use
#nameOf(@item)to get the current property name.
Examples
Section titled “Examples”Two passes through a variable
Section titled “Two passes through a variable”{ "someArray": [ { "someInteger": 1 }, { "someInteger": 2 } ]}{ "#foreach(@x in $.someArray) into @tempResult": [ { "intermediateValue": "@x.someInteger * 2" } ], "#foreach(@x;@i in @tempResult) into 'actualResult'": [ { "finalValue": "@x.intermediateValue + 5", "currentIndex": "@i" } ]}{ "actualResult": [ { "finalValue": 7, "currentIndex": 0 }, { "finalValue": 9, "currentIndex": 1 } ]}