Answers.Body: <p>I think you're checking correctly with <code>{% if variantImage not in productGalleryArray %}</code> (this should check if <code>variantImage</code> isn't already in the array), but your code doesn't seem to set a <code>productGalleryArray</code> array in the global scope. Variables initially set inside <code>for</code> loops are scoped only to that <code>for</code> loop.</p>
  
  <p>So I think you just need to declare an empty array at the top of your code <em>outside</em> and <em>before</em> the first <code>for</code> loop:</p>
  
  <pre><code>{% set productGalleryArray = [] %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-09-25T14:42:53.500Z
Answers.Text: Sorry, I probably should have added this array to the question, as this is already in there. So I'm still having the issue :(
Answers.UserDisplayName: Jason Mayo
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-09-22T17:41:01.690Z
Answers.LastActivityDate: 2017-09-22T17:41:01.690Z
Answers.UserDisplayName: Ryan
Body: <p>I'm trying to remove duplicate image objects from an array, but can't figure out why it won't find it:</p>
  
  <pre><code>{% for image in product.productGallery %}
      {% set productGalleryArray = productGalleryArray|merge([image]) %}
  {% endfor %}
  
  {% for variant in product.variants %}
      {% set variantImage = variant.productVariantImage.first() %}
      {% if variantImage|length and variantImage not in productGalleryArray %}
          {% set productGalleryArray = productGalleryArray|merge([variantImage]) %}
      {% endif %}
  {% endfor %}
  
  {% if productGalleryArray|length %}
      &lt;div class="slideshow"&gt;
          {% for image in productGalleryArray %}
              &lt;img src="{{ image.url }}" alt="{{ image.title }}"&gt;
          {% endfor %}
      &lt;/div&gt;
  {% endif %}
  </code></pre>
  
  <p>Essentially what I'm trying to do is have a <code>productGallery</code> field which you can add images to, but also add an image to the product variant via <code>productVariantImage</code>. </p>
  
  <p>This can cause the issue where by theres duplicate of the same image being output from the slideshow after it's merged the two fields together.</p>
  
  <p>So I need to ideally:</p>
  
  <ul>
  <li>Remove the duplicates before the array is merged, OR</li>
  <li>Find and remove duplicates after the array is merged (Via a Twig filter?)</li>
  </ul>
  
Tags: <templating><assets><image>
Title: Checking for duplicates in array
ThreadText: # Checking for duplicates in array
  
  (Asked by: username_0 on 2017-09-22)
  
  I'm trying to remove duplicate image objects from an array, but can't figure out why it won't find it:
  
  ```
  {% for image in product.productGallery %}
      {% set productGalleryArray = productGalleryArray|merge([image]) %}
  {% endfor %}
  
  {% for variant in product.variants %}
      {% set variantImage = variant.productVariantImage.first() %}
      {% if variantImage|length and variantImage not in productGalleryArray %}
          {% set productGalleryArray = productGalleryArray|merge([variantImage]) %}
      {% endif %}
  {% endfor %}
  
  {% if productGalleryArray|length %}
      <div class="slideshow">
          {% for image in productGalleryArray %}
              <img src="{{ image.url }}" alt="{{ image.title }}">
          {% endfor %}
      </div>
  {% endif %}
  
  ```
  
  Essentially what I'm trying to do is have a `productGallery` field which you can add images to, but also add an image to the product variant via `productVariantImage`.
  
  This can cause the issue where by theres duplicate of the same image being output from the slideshow after it's merged the two fields together.
  
  So I need to ideally:
  
  * Remove the duplicates before the array is merged, OR
  * Find and remove duplicates after the array is merged (Via a Twig filter?)
  
  **Tags:** templating, assets, image
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-09-22. Score: 1
  
  I think you're checking correctly with `{% if variantImage not in productGalleryArray %}` (this should check if `variantImage` isn't already in the array), but your code doesn't seem to set a `productGalleryArray` array in the global scope. Variables initially set inside `for` loops are scoped only to that `for` loop.
  
  So I think you just need to declare an empty array at the top of your code *outside* and *before* the first `for` loop:
  
  ```
  {% set productGalleryArray = [] %}
  
  ```
  
  > **Comments:**
  > **username_0**: Sorry, I probably should have added this array to the question, as this is already in there. So I'm still having the issue :(  
  
  ---
  
  