Skip to content

#groupBy

Groups an array’s elements by a selected value into key and results pairs.

#groupBy(array, @x: key)
Category Grouping and aggregationValid in Property value
Name Type Description
array path | array The source array.
key lambda Selects the grouping value.

array — An array of { key, results } objects.

source.json
{
"someArray": [
{ "groupingProperty": "A", "valueProperty": 1 },
{ "groupingProperty": "A", "valueProperty": 2 },
{ "groupingProperty": "B", "valueProperty": 3 }
]
}
transformer.json
{
"groupedArray": "#groupBy($.someArray, @x: @x.groupingProperty)"
}
output.json
{
"groupedArray": [
{
"key": "A",
"results": [
{ "groupingProperty": "A", "valueProperty": 1 },
{ "groupingProperty": "A", "valueProperty": 2 }
]
},
{
"key": "B",
"results": [ { "groupingProperty": "B", "valueProperty": 3 } ]
}
]
}

Try it in the playground →

#summarizeWith · #distinct