pop

Removes and returns the last element from an array. Modifies the original array in place.

{% assign last_item = my_array | pop %}
{% log last_item %}

Syntax

{{ array | pop }}
Parameter
Description

array

Array to remove the last element from

Return Value

Returns the removed element. The original array is shortened by one element.

Examples

Pop the last element:

{% assign colors = "red,green,blue" | split: "," %}
{% assign last = colors | pop %}
{% log last %}
{% log colors %}

Output:

blue
["red", "green"]

Process items from the end:

Use with the pop tag for named assignment:

Notes

  • Modifies the original array — the popped element is removed permanently

  • Returns the input unchanged if it is not an array

  • There is also a {% pop array as variable %} tag that does the same thing with a clearer syntax

  • See also: push

Last updated