Arrays

Solved
GT5
Tourist
4 1 0

Hi,

I'm new with liquid and I have been trying to do an array with my product tags which I found how to do it. However, I would like to remove the duplicate. This is what I have, my if.. contains statement doesn't work.

{% assign tags = '' | split: '' %}
{% for product in collections.all.products %}

{%if tags contains product.tags %}
{%else%}
{% assign tags = tags | concat: product.tags %}
{%endif%}
{% endfor %}

 

0 Likes
Ninthony
Shopify Partner
2242 338 895

I don't quite understand what you're doing here. You're assigning a string to a variable called tags with a split in it?

 

{% assign tags = " | split: " %}

{{ tags }}

// The above would output this string:
// | split:

 

 

You would like to remove the duplicate you said. Duplicate what? Your tags are already an array. You can loop through them:

 

{% for tag in product.tags %}
{{ tag }}
{% endfor %}

 

 

Or you can check them for a specific tag right away:

 

{% if product.tags contains 'long sleeve' %}
// do something
{% endif %}

 

 

Can you better explain what you're trying to do?

If my solution helped you, please like it and accept it as the solution!
If you'd like to make any edits to your store, please send me a personal message and we can discuss what you'd like to accomplish :D
0 Likes
GT5
Tourist
4 1 0

This is an accepted solution.

Hi,

Thank you for your reply! I was pushing all my tags in an array, however, there were rduplicate tags. But, I found out how to remove duplicate element by using :

{{ fruits | split: ' ' | uniq | join: ' ' }}

0 Likes
Ninthony
Shopify Partner
2242 338 895

Ohhh ok, I see. Glad you got it figured out. You should mark your reply as the solution, not mine : )

If my solution helped you, please like it and accept it as the solution!
If you'd like to make any edits to your store, please send me a personal message and we can discuss what you'd like to accomplish :D
0 Likes