Skip to content

#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 ]
Category Loops and scopingValid in Property name
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.

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.
source.json
{
"someArray": [ { "someInteger": 1 }, { "someInteger": 2 } ]
}
transformer.json
{
"#foreach(@x in $.someArray) into @tempResult": [ { "intermediateValue": "@x.someInteger * 2" } ],
"#foreach(@x;@i in @tempResult) into 'actualResult'": [
{
"finalValue": "@x.intermediateValue + 5",
"currentIndex": "@i"
}
]
}
output.json
{
"actualResult": [
{ "finalValue": 7, "currentIndex": 0 },
{ "finalValue": 9, "currentIndex": 1 }
]
}

Try it in the playground →

#nameOf · #select · #using