Hi Guys, I am trying to create object using 2 metafields present on the collection. I am able to create the object but when I try to insert in an array (i.e to finally get an array with objects) I always get empty array. I believe there is some thing small mistake I am not able to identify. Below is my code Can someone please help:
{% assign collectionImageList = collection.metafields.custom.testing_list_image.value %}
{% assign collectionValueList = collection.metafields.custom.testing_list_value.value %}
{% assign objects = ââ | json %}
{% assign i = 0 %}
{% for image in collectionImageList %} {% assign bannerWithLink = â{âimmagesrcâ: "â | append: image.src | append: â", âbannerlinkâ: "â | append: collectionValueList[i] | append: â"}â | json %}
{% assign objects = objects | push: bannerWithLink | json %}
{% assign i = i | plus: 1 %}
{% endfor %}
{{ objectsArray }}
Can anyone help me with in solving above problem. It would be of great help!
Hi there!
Maybe Iâm late, but in your code I see a couple of things:
-
You reference objectsArray but I donât see this variable is declared anyway in the code above.
-
You are using âpushâ as a filter, but as far as I know, this is not a valid Liquid filter, so I donât think that is doing what you expect it to do, like in Javascrip. Liquid in general is not made to work with arrays like other languages do.
If you are looking to concatenate or modify arrays in Liquid, I recommend this post, that actually helped me in a similar case.
Building better arrays in Liquid
1 Like
Hey Biocule!
I know this might be a bit late for you but hopefully this helps out anyone else that comes through.
I think i see what youâre trying to do and itâs worthing noting that this isnât my solution but something I picked up from Braze, see below:
-- initialise an empty array
{% assign my_new_array = "" | split: ""%}
{% for item in my_array %}
-- apply whatever filtering you'd like
{% if item.thing == 'something' %}
-- this is the clever bit
-- you can use concat (not append) which joins two arrays, so instead of appending
-- the item directly, you slice it out which creates an array of 1 and concat the rest
-- of the array back to it!
{% assign my_new_array = my_array | slice: forloop.index0 | concat: my_new_array %}
{% endif %}
{% endfor %}
{% assign my_new_array = my_new_array | reverse %}
2 Likes
Thank you , this helped me!