liquid
liquid copied to clipboard
Map items at specific index
If I have an array of arrays, or a mapping, I'm looking for a version of map that will get me the items at specific index for the subarrays, or all the items of a key->item mapping.
For example, if I have an arr.yml file that looks like this:
key1: "item1"
key2: "item2"
or
- ['key1', "item1"]
- ['key2', "item2"]
I don't see any way to get just [ "item1", "item2" ].
Something like the following would be very useful:
{{ arr | index: 1 }} or {{ arr | map: '[1]' }}
I can't find any current way of doing this, except to use Jekyll support for push, i.e.:
{% assign tmp = "" | split: "," %}
{% for p in arr %}
{% assign tmp = tmp | push: p[1] %}
{% endfor %}
https://shopify.dev/api/liquid/objects/for-loops
{% for product in collections.frontpage.products %}
{{ forloop.index }}
{% else %}
// no products in your frontpage collection
{% endfor %}