CMS Development

Amit_95
Participant | Platinum Partner
Participant | Platinum Partner

Comparing two arrays, checking for matching values

SOLVE

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

Comparing two arrays, checking for matching values

SOLVE

Hey @Amit_95 

 

Have you tried "intersect"?

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

 

 

{{ topics|intersect(all_tags) }}

 

Kevin Cornett - Sr. Solutions Architect @ BridgeRev

View solution in original post

2 Replies 2
Kevin-C
Solution
Recognized Expert | Partner
Recognized Expert | Partner

Comparing two arrays, checking for matching values

SOLVE

Hey @Amit_95 

 

Have you tried "intersect"?

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

 

 

{{ topics|intersect(all_tags) }}

 

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
sharonlicari
Community Manager
Community Manager

Comparing two arrays, checking for matching values

SOLVE

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 


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




0 Upvotes