group_by

Groups an array elements based on item property value.

{% json employees %}
  [
    {
      "first_name": "Ann",
      "last_name": "Smith",
      "position": "Accountant",
      "city": "New York",
      "age": 25
    },
    {
      "first_name": "Adam",
      "last_name": "Fox",
      "position": "Salesman",
      "city": "New Jersey",
      "age": 35
    },
    {
      "first_name": "Angela",
      "last_name": "Newman",
      "position": "Accountant",
      "city": "Boston",
      "age": 43
    }
  ]
{% endjson %}
{% assign group_by_position_result = employees | group_by: "position" %}
{{ group_by_position_result | log }}

Results in following array logged to console:

[
  {
    "name": "Accountant",
    "items": [
      {
        "first_name": "Ann",
        "last_name": "Smith",
        "position": "Accountant",
        "city": "New York",
        "age": 25
      },
      {
        "first_name": "Angela",
        "last_name": "Newman",
        "position": "Accountant",
        "city": "Boston",
        "age": 43
      }
    ]
  },
  {
    "name": "Salesman",
    "items": [
      {
        "first_name": "Adam",
        "last_name": "Fox",
        "position": "Salesman",
        "city": "New Jersey",
        "age": 35
      }
    ]
  }
]

Last updated