Comparing two arrays, checking for matching values

SOLVE
Amit_95
Member | Platinum Partner

I have a repeater field which allows user to add text.

 

I've created an array which stores all these repeater values into an array called topics.

 

{% set topics = [] %}

<!-- append topics from repeater to array -->
{% for item in module.add_topics.topics %}
  {% set topic_option = item|striptags %}
  {% do topics.append( topic_option ) %}
{% endfor %}

<!-- dumping data 
{{ topics }}

[Data, Accounting, Insight]
--> 

 

The above is basically a predefined list. I now want to compare values in the above array to all blog tags that exist.

 

Demo:

 

{% set all_tags = blog_topics('default' , 250) %}

{{ all_tags }}

<!-- above prints: [Accounting, Press, Data] -->

 

So from the above two arrays, Data and Accounting are in both. I now want to create another array which has these matching values.

 

How can I go about this?

0 Upvotes
1 Accepted solution

Accepted Solutions
Kevin-C
Solution
Recognized Expert | Partner

Hey @Amit_95 

 

Have you tried "intersect"?

Using that filter it should return a dict with the matching objects.

 

 

{{ topics|intersect(all_tags) }}

 

View solution in original post

2 Replies 2
sharonlicari
Community Manager

Hey @Amit_95    

 

Thank you for sharing this. I'll tag a few experts that can share their expertise.  

 

Hey @Kevin-C @amwilie @John  any tips you can share with @Amit_95?   

 

Thank you 🙂

Sharon 


¿Sabías que la Comunidad está disponible en Español?
¡Participa hoy en conversaciones en el idioma de tu preferencia,cambiando el idioma en tus configuraciones!

Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !


0 Upvotes
Kevin-C
Solution
Recognized Expert | Partner

Hey @Amit_95 

 

Have you tried "intersect"?

Using that filter it should return a dict with the matching objects.

 

 

{{ topics|intersect(all_tags) }}

 

View solution in original post