Solved

How to remove duplicate product tags in liquid arrays?

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

 

Accepted Solution (1)
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: ' ' }}

View solution in original post

Replies 3 (3)

Ninthony
Shopify Partner
2330 350 1024

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 😄
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: ' ' }}

Ninthony
Shopify Partner
2330 350 1024

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 😄