liquid icon indicating copy to clipboard operation
liquid copied to clipboard

Map items at specific index

Open sameersingh opened this issue 5 years ago • 1 comments

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 %}

sameersingh avatar Dec 26 '19 19:12 sameersingh

https://shopify.dev/api/liquid/objects/for-loops

{% for product in collections.frontpage.products %}
  {{ forloop.index }}
{% else %}
  // no products in your frontpage collection
{% endfor %}

seadfeng avatar Apr 11 '22 15:04 seadfeng