Search code examples
javascriptfor-looptwilioliquidtwilio-studio

Using an increment to access array Twilio Studio


I am working with two widgets for the purposes of this question: The Set variables widget, and the split based on the widget. I want to iterate over my array which I am passing in (flow.data.arr):

I am trying in set variables:

key: increment
value: {% if flow.variables.increment %}{{flow.variables.increment | plus:1}}{% else %}0{% endif %}

and in the split based on:

condition: flow.data.arr[increment].nestedValue isNotBlank

I have tested with numbers and if I insert a 0 or 1 then it works perfectly. My assumption is that it is evaluating the increment var as a string, as when I had included spaces inside of the if logic, it added those spaces inside of the string.

SO If my assumption is correct, how can I make Twilio evaluate increment as a number? If my assumption is wrong how can I achieve this "for loop" style iteration?


Solution

  • Even though Twilio should be storing the variable as a number, Twilio will store it as a string. You have to do explicit casting before using the number to index the array.

    In the split based on you can use:

    condition:
    {% assign i=flow.variables.increment | plus:0 %}
    {{flow.data.arr[i].nestedValue}} isNotBlank
    

    To achieve the desired result. Adding 0 will cast it to a number at the moment of use.