Skip to content

Compact

Deletes any nil items from an array.

Example

Data
json
{
  "names": [
    "YouCan",
    "Amine",
    nil
    "Product",
  ]
}
liquid
Original names:

{% for name in names -%}
  - {{ name }}
{%- endfor %}

Names with compact filter:

{%- assign names = names | compact -%}
{% for name in names -%}
  - {{ name }}
{%- endfor %}

Output

Original names:

  • YouCan
  • Amine
  • Product

Names with compact filter:

  • YouCan
  • Amine
  • Product