Answers.Body: <p>Using the <a href="https://twig.symfony.com/doc/2.x/filters/map.html" rel="nofollow noreferrer"><code>|map</code></a> filter would work nicely here:</p>
  <pre><code>{% set variantIds = product.variants|map(variant =&gt; variant.id) %}
  
  {% if wheelSpacer in variantIds %}
      ...
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-07T14:10:34.870Z
Answers.LastActivityDate: 2021-05-07T14:10:34.870Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.Body: <p>You could use <a href="https://twig.symfony.com/doc/3.x/filters/filter.html" rel="nofollow noreferrer">filter</a> to get only the variant that matches the <code>wheelSpacer</code> variable. This will return an array, so you can use <a href="https://twig.symfony.com/doc/3.x/filters/first.html" rel="nofollow noreferrer">first</a> to get the first matching variant (since presumably <code>product.variants</code> won't contain duplicate variants). This has the added benefit of getting you the matching variant directly, instead of it's ID:</p>
  <pre><code>{% set variant = product.variants|filter(variant =&gt; variant.id == wheelSpacer)|first %}
  {% if variant %}
      {# ... #}
  {% endif %}
  </code></pre>
  <p>As a sidenote, if your <code>variants</code> field is an <a href="https://craftcms.com/docs/3.x/entries-fields.html" rel="nofollow noreferrer">Entries field</a>, you'll want to use <code>product.variants.all()</code> instead.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-07T14:11:17.650Z
Answers.LastActivityDate: 2021-05-07T14:11:17.650Z
Answers.UserDisplayName: MoritzLost
Body: <p>Is there an easier way to achieve this?
  Perhaps using a <code>|filter</code> function?</p>
  <pre><code>{% set variantIds = [] %}
  {% for variant in product.variants %}
    {% set variantId = variant.id %}
    {% set variantIds = variantIds|merge([variant.id]) %}
  {% endfor %}
  
  {% if wheelSpacer in variantIds %}
    this spacer
  {% endif %}
  
  </code></pre>
  
Tags: <templating><array>
Title: Checking if a value is in an array of IDS - using a filter?
ThreadText: # Checking if a value is in an array of IDS - using a filter?
  
  (Asked by: username_0 on 2021-05-07)
  
  Is there an easier way to achieve this?
  Perhaps using a `|filter` function?
  
  ```
  {% set variantIds = [] %}
  {% for variant in product.variants %}
    {% set variantId = variant.id %}
    {% set variantIds = variantIds|merge([variant.id]) %}
  {% endfor %}
  
  {% if wheelSpacer in variantIds %}
    this spacer
  {% endif %}
  
  ```
  
  **Tags:** templating, array
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2021-05-07. Score: 3 | Accepted answer
  
  Using the [`|map`](https://twig.symfony.com/doc/2.x/filters/map.html) filter would work nicely here:
  
  ```
  {% set variantIds = product.variants|map(variant => variant.id) %}
  
  {% if wheelSpacer in variantIds %}
      ...
  {% endif %}
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2021-05-07. Score: 4
  
  You could use [filter](https://twig.symfony.com/doc/3.x/filters/filter.html) to get only the variant that matches the `wheelSpacer` variable. This will return an array, so you can use [first](https://twig.symfony.com/doc/3.x/filters/first.html) to get the first matching variant (since presumably `product.variants` won't contain duplicate variants). This has the added benefit of getting you the matching variant directly, instead of it's ID:
  
  ```
  {% set variant = product.variants|filter(variant => variant.id == wheelSpacer)|first %}
  {% if variant %}
      {# ... #}
  {% endif %}
  
  ```
  
  As a sidenote, if your `variants` field is an [Entries field](https://craftcms.com/docs/3.x/entries-fields.html), you'll want to use `product.variants.all()` instead.
  
  
  ---
  
  