in_groups_of

Splits an array into smaller arrays (chunks) of a specified size. Useful for batching API calls, paginating data, or processing items in fixed-size groups.

{% assign batches = products | in_groups_of: 50 %}
{% for batch in batches %}
  {% log "Batch size: " | append: batch.size %}
{% endfor %}

Syntax

{{ array | in_groups_of: size }}
Parameter
Description

array

Array to split into groups

size

Maximum number of elements per group

Return Value

Returns an array of arrays. Each inner array contains at most size elements. The last group may contain fewer elements if the array length is not evenly divisible.

Examples

Batch API calls in groups of 10:

{% assign batches = product_ids | in_groups_of: 10 %}
{% for batch in batches %}
  {% assign ids = batch | join: "," %}
  {% log "Processing batch: " | append: ids %}
{% endfor %}

Process GraphQL mutations in batches of 250:

Split CSV rows for parallel processing:

Paginate results:

Notes

  • The last group may be smaller than the specified size

  • Returns an empty array if the input is empty

  • Useful in combination with run to dispatch batched work asynchronously

Last updated