Comparing two arrays, checking for matching values

Amit_95
Participant | Platinum Partner
Participant | 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
Kevin-C
Solution
Recognized Expert | Partner
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
Kevin-C
Solution
Recognized Expert | Partner
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) }}

 

sharonlicari
HubSpot Alumni
HubSpot Alumni

Hey @Amit_95    

 

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

 

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

 

Thank you 🙂

Sharon 


loop Loop Marketing is a new four-stage approach that combines AI efficiency and human authenticity to drive growth.
Learn More

0 Upvotes