Solved

How to find length of an array ?

  • 22 July 2021
  • 3 replies
  • 754 views

Badge +2

Hello, 

 

I am having troubles writing an email following the condition whether the array contains one element or several elements. 

I would like to build this condition : 

if event.Items.length > 1, then do something otherwise do something else. 

I can’t find a liquid expression implicating length or size to define an array working in Klaviyo

I tried {{ my_array.size }} but it does not work...

Thanks for your answers !

icon

Best answer by retention 22 July 2021, 18:44

View original

3 replies

Userlevel 7
Badge +57

@beckso  - I haven’t tried if this would work for an array, but there are two “Filter” that you can try to see if it applies.  You may have to cast your Array into a List with a loop first if it doesn’t work with an array datatype.  

length - Returns the length of the value. This works for both strings and lists.

Example: 

{{ value|length }}

length_is - Returns True if the value’s length is the argument, or False otherwise.

Example:

{{ value|length_is:"4" }}

Here is some resources to dig in:

Badge +2

Thanks for that, indeed “length” filter should work. 

Could you please explain how to cast the array into a list ? I can’t manage to do that. 

I would like to count the number of element in my client’s cart when he starts checkout : 

Thanks for your help!

Ines

Userlevel 7
Badge +60

Hey Ines (@beckso),

Do you mind sharing your use case in what it is you are trying to accomplish? 

Often times the Started Checkout metric should already be passing an Item Count variable that would be counting the numbers of items a customer has in their cart when they triggered the event. For example, if you were integrated with Shopify and using the Shopify Started Checkout metric for your abandoned cart flow, you can use the {{ event|lookup:'Item Count' }} syntax to print the number of products a customer had in their cart when they triggered the Started Checkout event. 

To learn more about leveraging the variables and data passed by your events to personalize your emails, I would suggest taking a look at the below Help Center articles. These articles also touch upon the subject of using variables filters as @retention had suggested prior.

I hope this helps!

David

Reply