#groupBy
Groups an array’s elements by a selected value into key and results pairs.
#groupBy(array, @x: key)Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
array |
path | array |
The source array. |
key |
lambda |
Selects the grouping value. |
Returns
Section titled “Returns”array — An array of { key, results } objects.
Examples
Section titled “Examples”Group by a property
Section titled “Group by a property”{ "someArray": [ { "groupingProperty": "A", "valueProperty": 1 }, { "groupingProperty": "A", "valueProperty": 2 }, { "groupingProperty": "B", "valueProperty": 3 } ]}{ "groupedArray": "#groupBy($.someArray, @x: @x.groupingProperty)"}{ "groupedArray": [ { "key": "A", "results": [ { "groupingProperty": "A", "valueProperty": 1 }, { "groupingProperty": "A", "valueProperty": 2 } ] }, { "key": "B", "results": [ { "groupingProperty": "B", "valueProperty": 3 } ] } ]}