<p>Presumably you already have the code to retrieve the products you're after.</p>

<p>So just loop through your products and merge your brands in to an array.  </p>

<p>If the brands are just strings, just make an array of the strings, otherwise if they are relations, grab the ids.</p>

<p>Something along these (untested) lines:</p>

<pre><code>{% set brandStrings = [] %}
{% set brandIDs = [] %}

{% for product in products %}
    {# Either of these... #}
    {% set brandStrings = brandStrings|merge([product.brand]) %}
    {% set brandIDs = brandIDs|merge([product.brand.first().id]) %}
{% endfor %}

{# If strings, you've now got an array #}
{# If Ids, and you need to get the names, you can do something like... #}

{% set brands = craft.entries.id(brandIDs).find() %}

{% for brand in brands %}
    {% set brandStrings = brandStrings|merge([brand.title]) %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2016-02-29T16:49:03.120Z
I would recommend wrapping this with the {% cache %} tag to improve performance.
Maarten de Graaf
CC BY-SA 3.0
2016-03-04T15:59:19.097Z
Thanks for your response, i will check it out and see if it works.
Sebastián.
CC BY-SA 3.0
2016-03-16T01:18:29.927Z
@Sebastián if this works for you, please accept the answer.
Jeremy Daalder
CC BY-SA 3.0
2017-05-09T22:19:33.823Z
Hey @Sebastián, can you please accept Jeremy's answer if it helped you, or update your question if it's not yet solved? (You can also post your own answer here if you solved the problem in a different way.)
Matt Stein
CC BY-SA 3.0
2016-02-28T05:23:28.407Z
2016-02-28T05:23:28.407Z
Jeremy Daalder
<p>I´m working on a website of veterinary products. Some of the product fields are "category" and "brand".</p>

<p>I have a product list page that shows a list of products of certain category, and I have a JS list to filter by "brand". So, what I need is to get a list of the brands from the products of this specific category ( without repeating ), so I can create the JS filter.</p>

<p>Any ideas? :)</p>

<templating>
Get a brand list of products for Javascript filter
# Get a brand list of products for Javascript filter

(Asked by: username_0 on 2016-02-27)

I´m working on a website of veterinary products. Some of the product fields are "category" and "brand".

I have a product list page that shows a list of products of certain category, and I have a JS list to filter by "brand". So, what I need is to get a list of the brands from the products of this specific category ( without repeating ), so I can create the JS filter.

Any ideas? :)

**Tags:** templating

---

## 1 answers

### Answer by username_1 on 2016-02-28. Score: 2 | Accepted answer

Presumably you already have the code to retrieve the products you're after.
So just loop through your products and merge your brands in to an array.  
If the brands are just strings, just make an array of the strings, otherwise if they are relations, grab the ids.
Something along these (untested) lines:
{% set brandStrings = [] %}
{% set brandIDs = [] %}

{% for product in products %}
    {# Either of these... #}
    {% set brandStrings = brandStrings|merge([product.brand]) %}
    {% set brandIDs = brandIDs|merge([product.brand.first().id]) %}
{% endfor %}

{# If strings, you've now got an array #}
{# If Ids, and you need to get the names, you can do something like... #}

{% set brands = craft.entries.id(brandIDs).find() %}

{% for brand in brands %}
    {% set brandStrings = brandStrings|merge([brand.title]) %}
{% endfor %}

> **Comments:**
> **username_2**: I would recommend wrapping this with the {% cache %} tag to improve performance.  
> **username_0**: Thanks for your response, i will check it out and see if it works.  
> **username_1**: @Sebastián if this works for you, please accept the answer.  
> **username_3**: Hey @Sebastián, can you please accept Jeremy's answer if it helped you, or update your question if it's not yet solved? (You can also post your own answer here if you solved the problem in a different way.)  

---

