group_by - Liquid Filter Reference - 1.1.0

Filter Name group_by
Group Collections
Version 1.1.0

Description

Groups an array of objects by a given property

Examples

When a string property is provided, it groups the collection by the provided property:
{% new List products = '[{"name":"Fridgetastic 3000","type":"whitegoods"},{"name":"Coldinator 500","type":"whitegoods"},{"name":"Geronimo 9000","type":"computers"},{"name":"Y2K Amaze","type":"computers"}]' %}

{% assign array = products | group_by: "type" %}
{% for group in array %}({{ group.name }}){% for item in group.items %}[{{ item.name }}]{% endfor %}{% endfor %}
(whitegoods)[Fridgetastic 3000][Coldinator 500](computers)[Geronimo 9000][Y2K Amaze]
When a variable property is provided, it groups the collection by the provided variable property:
{% new List products = '[{"name":"Fridgetastic 3000","type":"whitegoods"},{"name":"Coldinator 500","type":"whitegoods"},{"name":"Geronimo 9000","type":"computers"},{"name":"Y2K Amaze","type":"computers"}]' %}
{% assign grouping_field = 'type' %}

{% assign array = products | group_by: grouping_field %}
{% for group in array %}({{ group.name }}){% for item in group.items %}[{{ item.name }}]{% endfor %}{% endfor %}
(whitegoods)[Fridgetastic 3000][Coldinator 500](computers)[Geronimo 9000][Y2K Amaze]

Back to Filter List

Back to Liquid Reference for 1.1.0

 

 
Back to Documentation