group_by

Groups an array of objects by a property value. Returns an array of groups, each with a name (the property value) and items (array of matching objects).

{% assign grouped = products | group_by: "vendor" %}
{% for group in grouped %}
  {% log group.name | append: ": " | append: group.items.size | append: " products" %}
{% endfor %}

Syntax

{{ array | group_by: property }}
Parameter
Description

array

Array of objects to group

property

Property name to group by (supports nested properties via dot notation)

Return Value

Returns an array of group objects:

Property
Type
Description

name

any

The property value shared by all items in this group

items

Array

Array of items that have this property value

Examples

Group orders by status:

{% assign grouped = orders | group_by: "financial_status" %}
{% for group in grouped %}
  {% log group.name | append: ": " | append: group.items.size | append: " orders" %}
{% endfor %}

Output:

Group products by vendor and process each group:

Group by nested property:

Count items per group:

Notes

  • Groups are returned in the order their keys are first encountered

  • Supports dot notation for nested properties: "node.status", "address.city"

  • For grouping with complex expressions, use group_by_exp

  • For faster performance with direct key access, use group_by_property or group_by_fast

  • See also: group_by_exp, group_by_property, group_by_fast

Last updated