Expand my Community achievements bar.

SOLVED

How to determine which field to show from an array

Avatar

Level 3

Hi,

 

We want to show the loyalty card number in e-mails to our customers. You can have multiple loyalty card numbers, so it is an array.

We want to show the loyalty card number that starts with 2616. How can I do that? 

 

{{profile._company.LoyaltyCards.LoyaltyCardNumber}} 

 

If I make an each and then add the field, it will show every loyalty card number, but I only want to show the one that starts with 2616.

1 Accepted Solution

Avatar

Correct answer by
Employee

You can try this


{% let idx = 0 %}
{{#each profile._company.LoyaltyCards as |p|}}
{%#if startsWith(p,"2616") and idx < 1 %}

{{p}}
{% let idx = idx + 1%}
{%/if%}{{/each}}



View solution in original post

3 Replies

Avatar

Employee

You can have a helper on the following lines - 

 

{{#each profile._company.LoyaltyCards.LoyaltyCardNumber as |p|}}

{%#if startsWith(p,"2616") %}

{{p}}

{%/if%}

{{/each}}

 

https://experienceleague.adobe.com/en/docs/journey-optimizer/using/content-management/personalizatio...

https://experienceleague.adobe.com/en/docs/journey-optimizer/using/content-management/personalizatio...

 

Avatar

Level 3

Hi, 

 

I tried this: 

{{#each profile._company.LoyaltyCards as|entry|}}

{%#if startsWith(entry.LoyaltyCardNumber,"2616")%} {{entry.LoyaltyCardNumber}}

 

{%else if doesNotStartWith(entry.LoyaltyCardNumber, "2616")%}
else {{entry.LoyaltyCardNumber}}

{%/if%} {{/each}}

The weird thing is, that it gives back this: 

2616069166373 else 2616069171476 2616069173081 2616069171506

While I defined condition 2 as doesNotStartWith. 

The profile can have multiple loyalty card numbers and in this case also multiple loyalty card numbers that start with 2616

Avatar

Correct answer by
Employee

You can try this


{% let idx = 0 %}
{{#each profile._company.LoyaltyCards as |p|}}
{%#if startsWith(p,"2616") and idx < 1 %}

{{p}}
{% let idx = idx + 1%}
{%/if%}{{/each}}