Array | Community
Skip to main content
Level 4
October 8, 2024
Solved

Array

  • October 8, 2024
  • 3 replies
  • 827 views

Can anyone help me with how to collect the first two elements of an array in AJO (Handlebars.js) and store them in two different fields?


{% let demo = "1,903~19.03" %}
{% let array = split(demo, "~") %}

{% let val1 = array[0] %}
{% let val2 = array[1] %}

val1 :{{ val1 }}
val2: {{ val2 }}

I am using this, but I am getting an error.

Output:-

val1 :false
val2: false

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by nnakirikanti

@ajo_wisdomchase 

Some time I love regex to solve situations like this.

 

{% let demo = "1,903~19.03~1213" %}
{% let first = regexGroup(demo,"^([0-9,\\.]+)~([0-9,\\.]+)", 1) %}
{% let second = regexGroup(demo,"^([0-9,\\.]+)~([0-9,\\.]+)", 2) %}
{{first}}
{{second}}

 

~cheers,

NN

3 replies

Mohan_Dugganab
Adobe Employee
Adobe Employee
October 8, 2024

You can take a look at this related thread https://experienceleaguecommunities.adobe.com/t5/journey-optimizer-questions/get-last-item-of-a-list-while-doing-personalization-in-ajo/td-p/649202 

 

{% let demo = "1,903~19.03" %} Second part {% let idx = indexOf(demo,"~" ) + 1 %} {%= substr(demo, idx, length(demo)) %} First part {% let idx = indexOf(demo,"~" ) %} {%= substr(demo, 0, idx) %}
nnakirikanti
Community Advisor
nnakirikantiCommunity AdvisorAccepted solution
Community Advisor
October 8, 2024

@ajo_wisdomchase 

Some time I love regex to solve situations like this.

 

{% let demo = "1,903~19.03~1213" %}
{% let first = regexGroup(demo,"^([0-9,\\.]+)~([0-9,\\.]+)", 1) %}
{% let second = regexGroup(demo,"^([0-9,\\.]+)~([0-9,\\.]+)", 2) %}
{{first}}
{{second}}

 

~cheers,

NN

Sukrity_Wadhwa
Community Manager
Community Manager
October 16, 2024

Hi @ajo_wisdomchase,

Were you able to resolve this query with the help of the given solutions or do you still need more help here? Do let us know. In case the given solutions were helpful, then kindly choose the one that helped you the most as the 'Correct Reply'.
Thanks!

Sukrity Wadhwa