Compare two arrays in liquid

Highlighted
New Member
5 0 0

In our site, have some logic that tags new blogs articles in a specific blog with an order.id. So this blog has posts with one order.id per post.

 

On customer record (account), I made a grid where I want to show all articles where article.tags matches customer.orders.

 

I made an array for all the customer's order ids, and trying to compare this array with the article.tags array and show only the articles where there are matches in the two arrays.

 

Please help!

 

This is on customers/account.liquid:

     <div class="table-wrap">
        <table class="full table--responsive">
          <thead>
            <tr>
              <th>POST NAME</th>
            </tr>
          </thead>
          <tbody>

             {% assign myorders = '' %}
        {% for order in customer.orders  %}
           {% capture myorders %}
              {{ myorders }} {{ order.id }}
           {% endcapture %}
            {% endfor %}

             {% for article in blogs.my-posts.articles  %}
         {% if article.tags contains myorders %}

                   <!--SHOW THE MATCHING ARTICLES HERE-->

                   <tr>
                     <td class="underline"><strong><a href="{{ article.url }}">{{ article.title | capitalize }}</a></strong></td>
                   <tr>

                 {% else %}

                     You have no posts.

                 {% endif %}
         {% endfor %}
          </tbody>
        </table>              
      </div> 

 

0 Likes
Highlighted
Shopify Partner
2471 128 403

When trying to figure things out you should DEBUG by outputting variables you think your making into <pre></pre> tags to verify them:

 {% capture myorders %}
              {{ myorders }} {{ order.id }}
           {% endcapture %}
  1. This will contain a lot of excess whitspace every loop
  2. This doesn't make an array it's making a very long unsorted string made of strings , that's then used to fuzzy match to another possibly unsorted string of strings.

Even separately #1&2 are making it so there will be probably never be a match to one tag.

When the loops is done using <pre>{{ myorders }}</pre> it'll probably look like the following not including line breaks:

<pre>          12435467124354657813254674653252132465713245467           </pre>

 

So unless you using a concatenated string of order.ids as the tag string on those articles it's gonna miss.

 

2 Options use either split filter or the map filter to create an array

 

If you want to create an array from a string you've constructed use the split filter with a delimiter characters you've chosen:

{% capture myorders %}{{-myorders-}}{{-order.id-}},{% endcapture %}
{% assign myordersArray= myorders | split:"," %}

Here i'm also using the whitespace control (dash - )

You could use {% unless forloop.index.last%},{%endunless%} to get rid of excess comma in the capture block

 

  map is liquids array-filter to create an array from another array based on a shared property of the items in that array:

 

{% assign myordersArray= customer.orders | map:'id' %}

 

Note: There is currently liquid tag or filter for array's to intersect or compare for simplifying the comparison|matching of 2 or more arrays.

 

Problem Solved? ✔️Accept and ? Like the solution so you can help others.
Buy me a coffee ☕ paypal.me/paulnewton or donate to eff.org
Confused? Busy? Buy a custom solution paull.newton+shopifyforum@gmail.com
0 Likes