reverse
array | reverse
Reverses the order of the items in an array.
Original order:
{{ collection.products | map: 'title' | join: ', ' }}
Reverse order:
{{ collection.products | reverse | map: 'title' | join: ', ' }}
Original order:
{{ collection.products | map: 'title' | join: ', ' }}
Reverse order:
{{ collection.products | reverse | map: 'title' | join: ', ' }}
{
"collection": {
"products": [
{
"title": "Draught of Immortality"
},
{
"title": "Glacier ice"
},
{
"title": "Health potion"
},
{
"title": "Invisibility potion"
}
]
}
}
Output
Original order:
Draught of Immortality, Glacier ice, Health potion, Invisibility potion
Reverse order:
Invisibility potion, Health potion, Glacier ice, Draught of Immortality
Anchor to Reversing strings
Reversing strings
You can't use the reverse
filter on strings directly. However, you can use the split
filter to create an array of characters in the string, reverse that array, and then use the join
filter to combine them again.
{{ collection.title | split: '' | reverse | join: '' }}
{{ collection.title | split: '' | reverse | join: '' }}
{
"collection": {
"title": "Sale potions"
}
}
Output
snoitop elaS
Was this page helpful?