collect() : Combine Values into One Array
Combine any number of values and collections into a single array. Each collection is added item by item; any other value is added as one item.
To read a property from every item in a collection, use eachOf().
On this page
Signature
collect(value1, value2, ...)
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
value1, value2, ... |
Any | Yes (at least one) | Values or collections to combine |
Returns
Type: Array
A new array with the items of every collection, and every other value, in order.
Examples
Combining Two Lists
Data:
builder.WithData("model", new
{
staff = new[] { "Ann", "Bob" },
contractors = new[] { "Cy", "Di" }
});
Template:
<p>Everyone: {{join(', ', collect(model.staff, model.contractors))}}</p>
<p>Headcount: {{count(collect(model.staff, model.contractors))}}</p>
Output:
<p>Everyone: Ann, Bob, Cy, Di</p>
<p>Headcount: 4</p>
Adding Single Values
<p>{{join(', ', collect('Manager', model.staff, 'Visitor'))}}</p>
Output:
<p>Manager, Ann, Bob, Visitor</p>
Totals Across Lists
<p>All costs: {{sum(collect(model.fixedCosts, model.variableCosts))}}</p>
Notes
- Collections are flattened one level - their items are added, not the collection itself
- The order of the parameters is kept
- Use eachOf() to extract a property from each item