GROUP

Groups items in an array

Description

Groups items in the given array using two formulas: one that defines the grouping criteria for each item, and one that creates the group items returned by this function.

First the grouping criteria formula is evaluated for each item in the array. All the items that evaluate to the equal _group key_ are grouped together.

Then the latter formula is evaluated for each _group_. You can use key in the formula to get the grouping key of the current group, and items to access all the items belonging to the current group.

Function signature

GROUP(array: Array, groupcriteria: Function, groupingformula: Function) => Array

Parameter

Description

Array

Array of items to group

Group criteria

Formula that returns a group key for the current item

Grouping formula

Formula that returns the final value for each "group" in the result array

Examples

allProducts = [
{
"name": "Duct tape",
"category": "Utility"
},
{
"name": "Swiss army knife",
"category": "Tools"
},
{
"name": "Glue",
"category": "Utility"
}
]

Formula

Return value

GROUP(allProducts, item.category, { category: key, products: items })

[{"category":"Utility","products":[{"name":"Duct tape","category":"Utility"},{"name":"Glue","category":"Utility"}]},{"category":"Tools","products":[{"name":"Swiss army knife","category":"Tools"}]}]

GROUP(allProducts, item.category, key + " category contains " + COUNT(items) + " product(s)")

["Utility category contains 2 product(s)","Tools category contains 1 product(s)"]

GROUP<product, index, category, products>(allProducts, product.category, { category: category, count: COUNT(products) })

[{"category":"Utility","count":2},{"category":"Tools","count":1}]