Skip to main content
Contributor I
July 22, 2021
Solved

How to find length of an array ?

  • July 22, 2021
  • 3 replies
  • 1193 views

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 !

    This topic has been closed for replies.
    Best answer by retention

    @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:

    3 replies

    retention
    Partner - Platinum
    retentionAnswer
    2025 Champion
    July 22, 2021

    @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:

    Joseph Hsieh // retentioncommerce.com // X: @retention
    becksoAuthor
    Contributor I
    July 23, 2021

    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

    David To
    Klaviyo Employee
    Klaviyo Employee
    July 23, 2021

    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