How to access a specific array element?
It is possible to use expression language to access specific items in an array if a numeric keys are used. The array can contain strings, number or objects.
Access array at index 1
{% {array_with_strings}[1] %} => "test_2"
Access array at index 1 and display value of "item"
{% {array_with_objects}[1]["item"] %} => "test_2"
Example JSON
{
"array_with_objects": [
{
"item": "test"
},
{
"item": "test_2"
}
],
"array_with_strings": [
"test",
"test_2"
]
}
Accessing sub-items in the array
For example, you need to access the value of a location name in your line items meta fields and set the condition to trigger the rule if there is at least one item with the value of location name "abc".
The expression would be:
count(filter({line_items}, 'line_item_metafields["location_name"]["value"] == "abc"')) > 0
Related articles you may find helpful
- Is it possible to conditionally change the component style or hide the component?
- List (array) functions
- Table Component
Updated on: 04/02/2025
Thank you!