flat

Flattens a nested array by a specified depth level. Useful when working with arrays of arrays, such as results from map on nested data.

{% assign all_tags = products | map: "tags" | flat %}
{% log all_tags %}

Syntax

{{ array | flat }}
{{ array | flat: depth }}
Parameter
Description

array

Array to flatten

depth

How many levels of nesting to flatten (default: 1)

Return Value

Returns a new array with nested arrays flattened to the specified depth.

Examples

Flatten one level (default):

{% assign nested = "1,2|3,4|5,6" | split: "|" | map: "split", "," %}
{% assign flattened = nested | flat %}
{% log flattened %}

Collect all tags from multiple products:

{% assign all_tags = products | map: "tags" | flat %}
{% assign unique_tags = all_tags | uniq %}
{% log unique_tags %}

Flatten deeply nested arrays:

Collect all variant SKUs across products:

Notes

  • Default depth is 1 — only the first level of nesting is flattened

  • Non-array values are wrapped in an array before flattening

  • Use higher depth values for deeply nested structures

Last updated